kit: `esbuild` causes import errors with `adapter-node`
Describe the bug
Same issue as https://github.com/sveltejs/kit/issues/2400
After https://github.com/sveltejs/kit/pull/6372, I get:
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function (x) {
if (typeof require !== "undefined")
return require.apply(this, arguments);
throw new Error('Dynamic require of "' + x + '" is not supported');
});
...
var crypto_1 = __importDefault(__require("crypto"));
Perhaps the problem is https://github.com/evanw/esbuild/issues/1921
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-sczcqj?file=README.md
Logs
No response
System Info
any
Severity
blocking all usage of SvelteKit
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 33 (15 by maintainers)
Commits related to this issue
- Move non-browser packages to dependencies https://github.com/sveltejs/kit/issues/6440#issuecomment-1232056594 — committed to jason0x43/simple-news by jason0x43 2 years ago
- Move non-browser packages to dependencies https://github.com/sveltejs/kit/issues/6440#issuecomment-1232056594 — committed to jason0x43/gameroom by jason0x43 2 years ago
- fix(esbuild): under the environment of esm will be `throw new Error('Dynamic require of "' + x + '" is not supported');` > Dependency location error, move all devDependencies to dependencies(https://... — committed to condorheroblog/auto-front-matter by condorheroblog 2 years ago
This is happening because we now bundle your
devDependencies
, so that they don’t need to be installed alongside your deployment.Your app contains some dependencies that can’t be bundled. I was able to get your repro to build and run by moving
mongoose
andhtml-minifier
fromdevDependencies
todependencies
.From @spheenik in https://github.com/sveltejs/kit/issues/6440#issuecomment-1236823838
I can confirm this fixed the issue in my build. I happen to have a chunk with exactly the same name
server/chunk-BHN6OJC3.js
(probably not a coincidence?), and adding the above code made the build work withnode build
afterwards.My particular case:
Error: Dynamic require of "stream" is not supported
node-fetch
@firebase/auth
_layout.svelte
dependencies
, including firebase, so I’m unsure why this gets included in the (server) chunks.Downgrading to adapter node next.87 for now solves the issue.
I had a similar issue too. Thanks @dlebech
This worked for me
@Rich-Harris
I am wrestling with this problem for quite a while now. The code that @AlexRMU mentioned is in a chunk file, in my case
build/server/chunk-BHN6OJC3.js
If you manually patch this file, and add the following directly above the above mentioned code
then this issue seems to be solved (it is on my side). Seems to be difficult to find an elegant solution though. That code is generated by esbuild. I tried to patch the adapter-node to by using esbuilds’s
banner
configuration, that however adds the banner to every generated chunk, not just the one. Maybe there’s some other nice fix.I added the banner like explained here: https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
adapter-node
doesn’t useesbuild
currently, so I’m going to close this in favor of the issue you just filed@AlexRMU Yes, i could but there is nothing spectacular to see, just adds the sveltekit plugin 😀
Solved my issue, I referenced my tailwindconfig in my src code which has led to the bundling of tailwindcss and all its dependencies in the production build. The module
picocolors
(dependency of tailwindcss) neededtty
which led to the error. Removed the import of values from the tailwindconfig and everything is working now.Having the same issue after upgrading to the newest versions of kit and adapter-node. But in my case it says
Error: Dynamic require of "tty" is not supported
when trying to run in production mode. So unfortunately I cannot just move anydevDependency
todependency
to fix the issue. Or am I missing something here?agree. i’ll just pin my version for adapter-netlify right now, but this continuing to be a bug/problem for netlify as far as i’m aware.
cc @brittneypostma fyi
So, https://github.com/sveltejs/kit/pull/6896 appeared and I think the problem with
Dynamic require
is solved.When I try to build all the dependencies, it appears: A lot of
Circular dependency: ...
andIt was solved by changing the max-old-space-size, but this is not always possible. Apparently, this is not a rollup problem, you need to wait for ebuild to solve their problems.
Edit: I figured out why external dependencies were not being respected in the esbuild. I was importing firebase as
@firebase/auth
instead offirebase/auth
, and I guess esbuild couldn’t figure out that the@
import was the same. Changing my imports tofirebase/X
(without the@
) seems to have resolved the issue for me, if I’m very diligent about putting libraries needed for the server in thedependencies
list.@Rich-Harris it’s not enough to move dependencies from
devDependencies
todependencies
. Edit: It might be, see above.Here’s a reproduction of the issue with a single dependency
firebase
and it doesn’t matter if it’s included independencies
ordevDependencies
: https://github.com/dlebech/sveltekit-6440The manual workaround mentioned above solves the issue in this repo as well, but is not feasible to include in automated pipelines.
The
banner
trick also doesn’t work, but I assume it’s because adapter-node esbuild config is hardcoded and doesn’t accept any config parameters.@theetrain Quick note, your reproduction repository looks like it has some issues, for example you’re trying to access
fetch
in page.server.js and layout.server.js from the server load event, butfetch
is not available in server load events – so it gives a warning and eventually leads to a runtime error “fetch is not a function”, even when running the dev server (I’m on Node 16.15)@theetrain
In my case, we’re still on sveltekit next.471 and adapter node next.87, i.e. before the introduction of esbuild for including dependencies.
@Rich-Harris As it is written in the readme in the reproduction, you need to add
to the build before
var __require = ...