kit: [adapter-node] Cannot use import statement outside a module

Describe the bug

In local, the build with the node adapter works perfectly but when deploying it in Plesk I’ve got the message :

import { assetsMiddleware, kitMiddleware, prerenderedMiddleware } from "./middlewares.mjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47

Reproduction

Don’t know what to give to you in order to reproduce that.

Logs

No response

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Xeon(R) Gold 6230R CPU @ 2.10GHz
    Memory: 191.33 MB / 7.75 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.9.1 - /usr/bin/node
    npm: 7.21.1 - /usr/bin/npm

Severity

blocking all usage of SvelteKit

Additional Information

No response

About this issue

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

Most upvoted comments

Both : I use node 16.9 in Plesk and locally. When I add the package.json with the "type": "module" info, I’ve got this error :

/usr/share/passenger/helper-scripts/node-loader.js:80
	return originalRequire.apply(this, arguments);
                        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/vhosts/mydomain/httpdocs/index.js from /usr/share/passenger/helper-scripts/node-loader.js not supported.
Instead change the require of index.js in /usr/share/passenger/helper-scripts/node-loader.js to a dynamic import() which is available in all CommonJS modules.
    at Module.require (/usr/share/passenger/helper-scripts/node-loader.js:80:25)
    at loadApplication (/usr/share/passenger/helper-scripts/node-loader.js:243:2)
    at setupEnvironment (/usr/share/passenger/helper-scripts/node-loader.js:214:2)
    at Object.<anonymous> (/usr/share/passenger/helper-scripts/node-loader.js:133:1) {
  code: 'ERR_REQUIRE_ESM'
}

I had exactly the same issue on Plesk running Node 17.4.0, resolved by creating a package.json in the build output thus:

{
  "type": "module"
}

And then pointing plesk towards an additional file (index.cjs) added to the build output thus:

(() => import('./index.js'))();

Not sure if this helps, but I just had the same error when not deploying the package.json along the build directory. I think it would be nice to document this.

Error when doing a docker build and docker run on this:

FROM node
COPY build build
CMD ["node", "build"]

All fine here:

FROM node
COPY build build
COPY package.json package.json
CMD ["node", "build"]

No problem. Makes also sense regarding the {"type": "module"} it brings and the error message. I added a small PR for the README.

Oh thank you @bluwy ! I will search there and if I found a solution, I will post it here

Both : I use node 16.9 in Plesk and locally. When I add the package.json with the "type": "module" info, I’ve got this error :

/usr/share/passenger/helper-scripts/node-loader.js:80
	return originalRequire.apply(this, arguments);
                        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/vhosts/mydomain/httpdocs/index.js from /usr/share/passenger/helper-scripts/node-loader.js not supported.
Instead change the require of index.js in /usr/share/passenger/helper-scripts/node-loader.js to a dynamic import() which is available in all CommonJS modules.
    at Module.require (/usr/share/passenger/helper-scripts/node-loader.js:80:25)
    at loadApplication (/usr/share/passenger/helper-scripts/node-loader.js:243:2)
    at setupEnvironment (/usr/share/passenger/helper-scripts/node-loader.js:214:2)
    at Object.<anonymous> (/usr/share/passenger/helper-scripts/node-loader.js:133:1) {
  code: 'ERR_REQUIRE_ESM'
}