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" when loading from ESModules package

See original GitHub issue

Bug report

Description / Observed Behavior

When I import SWR from a package using ESModules (type is set to "module" in the package.json), it fails to load SWR with the error in the title. For me this is specifically happening with a wrapper library for swr that I’m using, which uses ESM.

I think that this is because the esm build of swr is using the extension .esm.js not .mjs, so Node doesn’t realise it is using ESM (even though it is under import in the exports object in swr’s package.json, which might be a bug with node). In fact, renaming the entrypoint from index.esm.js to index.mjs fixes the issue.

The 0.5 build (which works) doesn’t use exports so Node will be importing the CJS build, which is probably why that version works.

Expected Behavior

The library should import fine, without Node complaining that it is using import statements.

Repro Steps / Code Example

All you need to do is import useSWR from swr in a package that requires swr>=1.0.0 and has its type set to module:

// index.js
import useSWR from "swr";
// package.json
{
  "type": "module",
  "dependencies": {
    "swr": "^1.1.2"
  }
}

Additional Context

SWR version: 1.1.2 (seems to effect every version >=1.0.0) Node verison: 14.17.6 (although it doesn’t work on 16.13.1 too)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
huozhicommented, Dec 31, 2021

I would expect everything that supports the exports object to be able to support mjs files, so maybe an alternative solution that works with both is to use .esm.js files for the module and .mjs files for the exports

Sounds like a good idea. I can try to see if there’s any workarounds for it 🙏

0reactions
shudingcommented, Dec 31, 2021

https://twitter.com/antfu7/status/1476438919000772609 there’re some discussions in this thread that might be helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Cannot use import statement outside a module
import statements are permitted only in ES modules. ... Warning: To load an ES module, set "type": "module" in the package.json or use...
Read more >
Fix "cannot use import statement outside a module" in ...
You're using a Node version < 13.2.0 · You're using Node without the proper package.json settings · You're using a browser version that...
Read more >
Use ES Modules in Node.js & Fix "cannot use import ...
Fix cannot use import statement outside a module error in Node.js ... In the package.json file, we need to add "type": "module" to...
Read more >
How to fix SyntaxError: Cannot use import statement outside a ...
Simply tell Node JS your project should use ES Modules instead of CommonJS. This can be done by adding 1 simple line of...
Read more >
JS Fix For "SyntaxError: Cannot Use Import Statement Outside ...
"SyntaxError: Cannot use import statement outside a module" is a common Node.js error when working with import to include modules or packages.
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