-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Describe the bug
After upgrading to SvelteKit 2.21.5 from SvelteKit 2.21.2, I am starting to get ReferenceError: require is not defined in ES module scope
whenever I import a package that uses require
in any capacity. This did not happen before SvelteKit 2.21.3.
I thought it might be an issue with specific packages or projects, but it's not. I tested three different projects, all with different dependencies, and the same problem occurred.
Examples of affected packages:
- marked (https://www.npmjs.com/package/marked)
- highlight.js (https://www.npmjs.com/package/highlight.js)
- @google/genai (https://www.npmjs.com/package/@google/genai)
- nodemailer (https://www.npmjs.com/package/nodemailer)
I concluded that the issue is caused by SvelteKit versions >= 2.21.3 because I kept downgrading, and once I went below 2.21.3, things worked as expected.
Reproduction
- Create a new SvelteKit project
- Install one or more of the packages mentioned above
- Import one or more of them in a server module (e.g.
+page.server.ts
, code inside a.server.ts
file, or files inside the$lib/server
directory) - Build your app
- Run the app and execute any code that would cause the module to be loaded
Logs
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '[REDACTED_PATH]/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at ExportsCache.has (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15490:20)
at Module.patchedRequire (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15618:22)
at Hook._require.Module.require (file://[REDACTED_PATH]/build/server/chunks/hooks.server-MojicEX3.js:15557:28)
at require (node:internal/modules/helpers:135:16)
at Object.<anonymous> ([REDACTED_PATH]/node_modules/highlight.js/lib/index.js:1:12)
at Module._compile (node:internal/modules/cjs/loader:1730:14)
at Object..js (node:internal/modules/cjs/loader:1895:10)
at Module.load (node:internal/modules/cjs/loader:1465:32)
at Function._load (node:internal/modules/cjs/loader:1282:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
System Info
System:
OS: Windows 11 10.0.26100
CPU: (28) x64 Intel(R) Core(TM) i7-14700K
Memory: 35.78 GB / 63.70 GB
Binaries:
Node: 22.16.0 - C:\Program Files\nodejs\node.EXE
npm: 11.4.1 - ~\AppData\Roaming\npm\npm.CMD
bun: 1.2.16 - ~\.bun\bin\bun.EXE
Browsers:
Edge: Chromium (137.0.3296.68)
Internet Explorer: 11.0.26100.1882
npmPackages:
@sveltejs/adapter-node: ^5.2.12 => 5.2.12
@sveltejs/kit: ^2.21.5 => 2.21.5
@sveltejs/vite-plugin-svelte: ^5.1.0 => 5.1.0
svelte: ^5.34.2 => 5.34.2
vite: ^6.3.5 => 6.3.5
Severity
blocking an upgrade
Additional Information
No response
anatoliy-t7, yaman-cruise, Cluster2a, vvihar, jpdom and 1 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
elliott-with-the-longest-name-on-github commentedon Jun 16, 2025
Hmm @eltigerchino is this another downstream of the bundling change? It appears like we're loading a
cjs
version when we're expecting anesm
version -- for example,marked
exports both but we wouldn't expect to see this error if we were loading theesm
one...yaman-cruise commentedon Jun 16, 2025
I can confirm this issue with the package @sentry/sveltekit.
eltigerchino commentedon Jun 17, 2025
I've tested with
marked
andhighlight.js
and can't seem to reproduce this. Can you please share a minimal reproduction so that we can know the exact conditions this bug occurs in?It should be related to https://github.com/sveltejs/kit/pull/13843/files#diff-beef34c36ed87d7835d0166655010910fba9949a6ef95042cb468d7d73423816 but that only bundles packages that declare a dependency on
@sveltejs/kit
.We had this issue with
@sentry/sveltekit
a week ago #13869 but that was due to theirexports
field pointing to the CJS version under thenode
condition, so we managed to fix that upstream. Even then, it was only bundled because it had a peer dep on kit, which none of these packages seem to have.abdelfattahradwan commentedon Jun 17, 2025
@eltigerchino Unfortunately, I am also unable to reproduce the issue in a clean, new SvelteKit project. However, I have two larger projects that exhibit the same problem described above. Is there a way to privately share one of them with you to avoid leaking the contents to the public?abdelfattahradwan commentedon Jun 17, 2025
I found the cause. It is because of both
@sveltejs/kit
(>=2.21.3
) and@sentry/sveltekit
.I had the following code in
hooks.client.ts
:And this code in
hooks.server.ts
:After I commented those out, I was able to build and run my application normally without encountering the errors reported in the original message.
For some reason, SvelteKit versions after
2.21.3
don't play nicely with@sentry/sveltekit
(or the other way around?).The reason I wasn't able to reproduce the issue is that the reproduction project didn't use
@sentry/sveltekit
. After adding it to the reproduction project, I began to encounter the same errors.Cluster2a commentedon Jun 17, 2025
Same problem here (svelteKit + sentry).
eltigerchino commentedon Jun 18, 2025
What version of
@sentry/sveltekit
are you using? This should have been fixed in https://github.com/getsentry/sentry-javascript/releases/tag/9.28.1 but there may be remaining issues causing the CJS version of Sentry to be bundled instead. Possibly related: getsentry/sentry-javascript#16586jeannemas commentedon Jun 18, 2025
I also encounter the issue, but my logs seems to indicate that it's related to
chokidar
this time.@sentry/sveltekit
9.29.0
@sveltejs/kit
2.21.5
It is interesting to note, that
chokidar
is not one of my dependencies, it is a dependency of one of my dependencies.Staying on
@sveltejs/kit@2.21.2
is the only way I've found to avoid the issue.eltigerchino commentedon Jun 18, 2025
We really need a minimal reproduction (in the form of a downloadable repository) because I can't reproduce this by installing
marked
and@sentry/sveltekit
.yaman-cruise commentedon Jun 18, 2025
It's not enough to install the packages. You also have to import them to a server file e.g. hooks.server.ts. Here is a minimal repo that creates the error: https://github.com/yaman-cruise/svelte-sentry
pnpm build
and you'll get the erroreltigerchino commentedon Jun 18, 2025
Yes, I've installed them and imported them into a server file.
This doesn't error for me after running
pnpm build
. Which Node version and operating system are you using when running the build? I'm running on WSL 2 Ubuntu 22.04.5 LTS and Node v18.20.822 remaining items