Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[adapter-node] Cannot use import statement outside a module #2410

Closed
pierre-H opened this issue Sep 12, 2021 · 17 comments · Fixed by #2724
Closed

[adapter-node] Cannot use import statement outside a module #2410

pierre-H opened this issue Sep 12, 2021 · 17 comments · Fixed by #2724

Comments

@pierre-H
Copy link

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

@pierre-H
Copy link
Author

pierre-H commented Sep 12, 2021

Editing the config with :

adapter: adapter({
  esbuild: (defaultConfig) => ({
    ...defaultConfig,
    format: 'cjs'
  })
})

Gives this error :

node:internal/modules/cjs/loader:1214
      throw new ERR_INVALID_ARG_VALUE('filename', filename,
      ^

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at createRequire (node:internal/modules/cjs/loader:1214:13)
    at node_modules/@sveltejs/adapter-node/files/shims.js (/var/www/vhosts/myhost/httpdocs/middlewares.js:1048:46)
    at __init (/var/www/vhosts/myhost/httpdocs/middlewares.js:29:50)
    at Object.<anonymous> (/var/www/vhosts/myhost/httpdocs/middlewares.js:16237:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    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 Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'ERR_INVALID_ARG_VALUE'
}

The error is from :

// node_modules/@sveltejs/adapter-node/files/shims.js
var import_module, import_meta;
var init_shims = __esm({
  "node_modules/@sveltejs/adapter-node/files/shims.js"() {
    import_module = __toModule(require("module"));
    init_install_fetch();
    import_meta = {};
    Object.defineProperty(globalThis, "require", {
      enumerable: true,
      value: (0, import_module.createRequire)(import_meta.url)
    });
  }
});

@dummdidumm
Copy link
Member

I don't know if you have to or can specify a package.json - if so, add "type":"module" to it. This helped me in a different deployment situation.

@pierre-H
Copy link
Author

I tried it but it doesn't work.

@benmccann benmccann changed the title [Node adaptater] Cannot use import statement outside a module [adapter-node] Cannot use import statement outside a module Sep 13, 2021
@bluwy
Copy link
Member

bluwy commented Sep 15, 2021

Searching around "Plesk + Node", looks like they have poor support for NodeJS versions and are generally outdated. Example. The minimum supported version by SvelteKit is ^12.20 || >=14.13, so unless you can upgrade Plesk's node version to latest, there's not much we can do here.

@pierre-H
Copy link
Author

I did upgrade to node 16 but doesn't still work.

@bluwy
Copy link
Member

bluwy commented Sep 15, 2021

Is it node 16 in Plesk or locally? Based on your description, it looks like Plesk is having trouble running ESM code, as it's running it in CJS context, which shouldn't be the case if package.json has type: "module" and it's running with the latest node version.

@pierre-H
Copy link
Author

pierre-H commented Sep 15, 2021

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

@bluwy
Copy link
Member

bluwy commented Sep 15, 2021

Thanks for the info. Looking at the first line: /usr/share/passenger/helper-scripts/node-loader.js:80, I'm not sure where this script comes from, but it's running your app using require(), which only works for CJS code. I'm not too familar with Plesk to know how to workaround this, but you could ask in their forums for how to load ESM code there.

@pierre-H
Copy link
Author

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

@pierre-H
Copy link
Author

For information : https://talk.plesk.com/threads/nodejs-execute-esm-code.362146/ (no answers yet)

@ubersan
Copy link
Contributor

ubersan commented Oct 30, 2021

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"]

@bluwy
Copy link
Member

bluwy commented Nov 1, 2021

@ubersan Is this docker setup related to Plesk? If so I think we can document this in the discussion @pierre-H opened above. I'm not sure if it fits here.

@ubersan
Copy link
Contributor

ubersan commented Nov 1, 2021

No, I don't think this is Plesk-specific. The docker setup just tells you that whenever you have a host where you have node installed and you only upload the build directory, you will get this error. To fix it you need to provide the package.json file. In my case the host was an aws ec2 instance. You can try it out on your local computer as well if you just copy the build directory into a folder and omit the package.json.

@bluwy
Copy link
Member

bluwy commented Nov 1, 2021

Thanks for the clarification. Yeah the package.json is needed together with the build for the {"type": "module"}. Perhaps @pierre-H can you confirm this is the case as well for your setup?

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.

It may be beneficial to note this under the node adapter readme. Feel free to send a PR!

@ubersan
Copy link
Contributor

ubersan commented Nov 2, 2021

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

@asos-tomp
Copy link
Contributor

asos-tomp commented Jan 21, 2022

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

@mike-tuxedo
Copy link

Great job, this worked for basic useage.
Sadly for the actual sveltekit demo app, it is unfortunately only partially working. Todos page doesn't work, calling https://yourdomain/todos/__data.json returns an 502.

Any idea how to fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants