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.

Cannot use import statement outside a module

See original GitHub issue

This is a followup on #2161. This issue had been fixed, but it was reintroduced in the latest sveltekit.

As asked, here is a reproduction: https://github.com/lovasoa/SvelteKit-2161

The stacktrace is

Cannot use import statement outside a module

/tmp/SvelteKit-2161/node_modules/@popperjs/core/dist/esm/popper.js:1
import { popperGenerator, detectOverflow } from "./createPopper.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)

@lovasoa can you file a new issue with a reproduction? I don’t have a way to reproduce and investigate at the moment

_Originally posted by @benmccann in https://github.com/sveltejs/kit/issues/2161#issuecomment-1085851247_

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

15reactions
Rich-Harriscommented, May 24, 2022

This is a bug in @popperjs/core — Node is behaving as it should. Popper should either

  • use an .mjs extension for all the modules in dist/esm/**/*.js, or
  • create a dist/esm/package.json with {"type":"module"}, or
  • (ideally) put {"type":"module"} in the root package.json

It should also declare its exports using the "exports" field in package.json.

As far as I can tell, the package has been superseded by floating-ui, which appears to do things differently, so maybe this is fixed already.

Either way, it can be worked around by populating Vite’s ssr.noExternal config:

/** @type {import('@sveltejs/kit').Config} */
const config = {
  kit: {
    vite: {
      ssr: {
        noExternal: ['@popperjs/core']
      }
    }
  }
};

export default config;
1reaction
nstuyvesantcommented, Sep 21, 2022

Dropping the import of the two enums solved the problem but I had to redefine them locally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript ...
Read more >
How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >
SyntaxError: Cannot use import statement outside a module
If you are using node application and want to use the import statement, then you will also get the " Cannot use import...
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