kit: Error: Dynamic require of "x" is not supported

Describe the bug

Upgrading to the latest version of sveltekit broke adapter-node for my application. Now the application works in development, but when packaged by adapter-node, it throws `` on start

Reproduction

It can be reproduced on lovasoa/sanipasse@79c310b88345773249ff0c1a9ff00594b712ee83

https://github.com/lovasoa/sanipasse/commit/79c310b88345773249ff0c1a9ff00594b712ee83

Logs

file:///home/olojkine/dev/sanipasse/build/middlewares.js:26
  throw new Error('Dynamic require of "' + x + '" is not supported');
        ^

Error: Dynamic require of "crypto" is not supported
    at __require (file:///home/olojkine/dev/sanipasse/build/middlewares.js:26:9)
    at node_modules/@peculiar/webcrypto/build/webcrypto.js (file:///home/olojkine/dev/sanipasse/build/middlewares.js:8119:19)
    at __require2 (file:///home/olojkine/dev/sanipasse/build/middlewares.js:32:44)
    at node_modules/isomorphic-webcrypto/src/index.js (file:///home/olojkine/dev/sanipasse/build/middlewares.js:10253:22)
    at __require2 (file:///home/olojkine/dev/sanipasse/build/middlewares.js:32:44)
    at node_modules/isomorphic-webcrypto/src/main.js (file:///home/olojkine/dev/sanipasse/build/middlewares.js:10262:22)
    at __require2 (file:///home/olojkine/dev/sanipasse/build/middlewares.js:32:44)
    at file:///home/olojkine/dev/sanipasse/build/middlewares.js:10271:46
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)

System Info

System:
    OS: Linux 5.13 Fedora 34 (Workstation Edition) 34 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 2.64 GB / 15.28 GB
    Container: Yes
    Shell: 5.1.0 - /bin/bash
  Binaries:
    Node: 16.5.0 - /usr/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 7.19.1 - /usr/bin/npm
  Browsers:
    Firefox: 91.0.2
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.46 
    @sveltejs/adapter-static: next => 1.0.0-next.18 
    @sveltejs/kit: next => 1.0.0-next.165 
    svelte: ^3.38.3 => 3.42.5 

Severity

blocking all usage of SvelteKit

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 22 (17 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve commented on that PR with more SvelteKit context to help esbuild maintainers understand why we need this.

I’ve also found https://github.com/evanw/esbuild/issues/946 which appears to be the main issue for why we needed this shim in the first place. I haven’t checked whether the other suggested workarounds in that issue (like the banner one, for example) will still help us here even with the change to __require.

The same thing after https://github.com/sveltejs/kit/pull/6372

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

I made a PR to esbuild to revert the change. See my comment there.

If I manually change the generated build/middlewares.js to add global.require = createRequire(import.meta.url); at the top, the app starts normally

If I add

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

before var __require, this will appear:

node:internal/modules/cjs/loader:933
  const err = new Error(message);
              ^

Error: Cannot find module '../lib/utils.js'
Require stack:
- ...\build\server\chunk-BHN6OJC3.js

in

// node_modules/.pnpm/uglify-js@3.17.0/node_modules/uglify-js/tools/node.js
var require_node2 = __commonJS({
  "node_modules/.pnpm/uglify-js@3.17.0/node_modules/uglify-js/tools/node.js"(exports) {
    var fs = __require("fs");
    exports.FILES = [
      __require.resolve("../lib/utils.js"),
      __require.resolve("../lib/ast.js"),
      __require.resolve("../lib/transform.js"),
      __require.resolve("../lib/parse.js"),
      __require.resolve("../lib/scope.js"),
      __require.resolve("../lib/compress.js"),
      __require.resolve("../lib/output.js"),
      __require.resolve("../lib/sourcemap.js"),
      __require.resolve("../lib/mozilla-ast.js"),
      __require.resolve("../lib/propmangle.js"),
      __require.resolve("../lib/minify.js"),
      __require.resolve("./exports.js")
    ];
    ...

Yup, all that should have been needed was folks upgrading their transitive esbuild dependency to 0.12.29, which didn’t require any action on SvelteKit’s part. But we’ve now updated to 0.13 anyway, so we should be good.

Maybe there can even be a temporary fix where the esbuild version is pinned ? This can be done without any implementation work, and would fix all the sveltekit projects that have been broken for two weeks now.

Also got this issue but with another package.

@lovasoa how about renaming title to Error: Dynamic require of "x" is not supported since it doesn’t really depend on which package you are importing? For me this happened with package tty, which was imported by supports_color package.

I updated each package separately in my project to find the cause of the issue and interestingly this is caused by eslint-plugin-svelte3 version 3.2.1. Works on version 3.2.0. @lovasoa also has his package updated in lock file.

Awesome! From Evan’s comment the other day, it didn’t sound like it was going to be fixed that quickly.

I do think it is worth making changes on our end rather than just closing this issue. We should probably try removing our own require shim from the Node adapter and bumping the minimum esbuild version.

We were hit by this today.

@dummdidumm

We just updated the package-lock.json of our project (rm -rf node_modules package-lock.json && npm i) : esbuild was updated to 0.12.29.

We deployed on our staging env (our build and deploy uses npm ci so it obeys to the lockfile) and it works again.

(strange thing: We were unable to reproduce the problem locally this morning but our staging env was broken)

esbuild just released a new version which contains a require shim which should fix this. I’m not sure if there’s anything else we need to do here, as the adapter-node package should load the latest esbuild version when installed.

I tried to update all the dependencies and just pin eslint-plugin-svelte3@3.2.0, and the problem is still present, so this is probably not where the bug is.