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

Most upvoted comments

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 and html-minifier from devDependencies to dependencies.

From @spheenik in https://github.com/sveltejs/kit/issues/6440#issuecomment-1236823838

If you manually patch this file, and add the following directly above the above mentioned code

import { createRequire } from "module";
const require = createRequire(import.meta.url);

then this issue seems to be solved (it is on my side).

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 with node build afterwards.

My particular case:

  • My specific error is Error: Dynamic require of "stream" is not supported
  • It looks like it appears during a bundled node-fetch
    • which in turn is imported from a bundled @firebase/auth
      • which in turn is imported via a _layout.svelte
  • I have everything that needs to run on the server in 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

Downgrading to adapter node next.87 for now solves the issue.

This worked for me

@Rich-Harris

Solved by adding require.

What does this mean?

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

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');
});

If you manually patch this file, and add the following directly above the above mentioned code

import { createRequire } from "module";
const require = createRequire(import.meta.url);

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 use esbuild 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) needed tty 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 any devDependency to dependency 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: ... and

<--- Last few GCs --->

[7236:000002396E15EFC0]   108147 ms: Scavenge 4009.4 (4096.6) -> 4008.8 (4107.3) MB, 6.4 / 0.0 ms  (average mu = 0.338, current mu = 0.329) allocation failure
...

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6FC137A1F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+114207
...

It 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 of firebase/auth, and I guess esbuild couldn’t figure out that the @ import was the same. Changing my imports to firebase/X (without the @) seems to have resolved the issue for me, if I’m very diligent about putting libraries needed for the server in the dependencies list.


@Rich-Harris it’s not enough to move dependencies from devDependencies to dependencies. 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 in dependencies or devDependencies: https://github.com/dlebech/sveltekit-6440

The 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, but fetch 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

What vite/esbuild config worked for you?

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

import { createRequire } from "module";
const require = createRequire(import.meta.url);

to the build before var __require = ...