question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
asos-tompcommented, 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'))();
2reactions
ubersancommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SvelteKit v1.0.0-next.146 SyntaxError: Cannot use import ...
I have the same issue when I start a new demo project. The error: Cannot use import statement outside a module in vscode....
Read more >
SyntaxError: Cannot use import statement outside a ...
I got the error from your question (Cannot use import statement outside a module). Importing validator/es/lib/isEmail supposedly only imports a ...
Read more >
Fix "cannot use import statement outside a module" in ...
This results in the error "Uncaught SyntaxError: cannot use import statement outside a module". If you've run into this error, it's possible ...
Read more >
esm
I try to use SvelteKit which generates ESM code but it seems that ... [adapter-node] Cannot use import statement outside a module ·...
Read more >
cannot use import statement outside a module - ItsJavaScript
The SyntaxError: cannot use import statement outside a module occurs if you have forgotten to add type="module" attribute while loading the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found