kit: hooks.server : ReferenceError: __filename is not defined in ES module scope
Describe the bug
Using @sveltejs/adapter-node and some function in hooks.server.ts, I get the following error:
file:///home/wighawag/dev/github.com/bug-reproduction/svelte-kit-hooks-server/build/server/chunks/hooks.server-8e68a0a6.js:335
if (fileName !== __filename) {
^
ReferenceError: __filename is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/wighawag/dev/github.com/bug-reproduction/svelte-kit-hooks-server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
(Use `node --trace-uncaught ...` to show where the exception was thrown)
Node.js v18.16.0
Reproduction
Repo: https://github.com/bug-reproduction/svelte-kit-hooks-server
git clone https://github.com/bug-reproduction/svelte-kit-hooks-server
cd svelte-kit-hooks-server
pnpm i
pnpm build
node build
Logs
file:///home/wighawag/dev/github.com/bug-reproduction/svelte-kit-hooks-server/build/server/chunks/hooks.server-8e68a0a6.js:335
if (fileName !== __filename) {
^
ReferenceError: __filename is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/home/wighawag/dev/github.com/bug-reproduction/svelte-kit-hooks-server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
(Use `node --trace-uncaught ...` to show where the exception was thrown)
Node.js v18.16.0
System Info
System:
OS: Linux 5.19 KDE neon 5.27 5.27
CPU: (16) x64 AMD Ryzen 7 PRO 6850U with Radeon Graphics
Memory: 17.08 GB / 30.13 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 18.16.0 - ~/.volta/tools/image/node/18.16.0/bin/node
Yarn: 4.0.0-rc.44 - ~/.volta/tools/image/yarn/4.0.0-rc.44/bin/yarn
npm: 9.5.1 - ~/.volta/tools/image/node/18.16.0/bin/npm
Browsers:
Brave Browser: 113.1.51.114
Chromium: 113.0.5672.126
Firefox: 112.0.1
npmPackages:
@sveltejs/adapter-node: ^1.2.4 => 1.2.4
@sveltejs/kit: ^1.5.0 => 1.20.0
svelte: ^3.54.0 => 3.59.1
vite: ^4.3.0 => 4.3.9
Severity
blocking all usage of SvelteKit
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 17 (8 by maintainers)
I ran into the same issue today. For some reason what fixed the issue was moving better-sqlite3 from “devDependencies” to “dependencies”.
It works because the root problem is that
devDependenciesare automatically bundled using Rollup, while normal dependencies are left as-is in the node_modules folder.This is mentioned briefly in the adapter-node docs:
https://kit.svelte.dev/docs/adapter-node#deploying
hmm, I am not sure how I can work around it.
I can patch it with
fileURLToPath(import.meta.url)but because it is a cjs file with things likeexports.getFileName = ...I get the error:Ha, yes that works, thanks @biowaffeln !
I guess that’s how vite detect whether it needs to do something
I close the issue then
i solved it by manually adding “bindings” to my dependencies, and it seems it works. Seems secondary dependencies are an issue