fix: Dumber fix for error assertions
#13894
Closing issue
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.tsfile, or files inside the$lib/serverdirectory) - 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
Pull request
fixes #13888
We've had issues in the past because isHttpError, isRedirect, and isActionFailure can start reporting false negatives when something somewhere decides to load two copies of the modules the classes are declared in. This switches to a tagged, structural approach that will survive these boundaries. The downside is that some library could throw { _tag: 'SvelteKitRedirect' } and isRedirect would report true, but... I mean, at that point, "hack the library at your own peril", I guess.
This would let us revert our prior attempt at externalizing dependencies, which has caused additional bugs.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Info
⚠️ No Changeset found
Latest commit: 65d0430
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
There's some internal code as well that do not use these helpers and directly uses the instanceof check. We'd have to make changes there too to account for 3rd-party libraries throwing their own instances of these classes.
Pro tip: You can prefix GitHub URLs of issues, PRs or discussions with svcl.dev/ to view them on this page! Also try it on a GitHub release ;)