ESM problem `Cannot use import statement outside a module`
See original GitHub issuePrerequisites
- [ x] Checked that your issue hasn’t already been filed by cross-referencing issues with the
faq
label - [ x] Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn’t just a feature that actually isn’t supported in the environment in question or a bug in your code.
- [ x] ‘Smoke tested’ the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
- Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
node node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.
Description
Steps to Reproduce
You need a test who use ES6 code from the node_modules. The esm does not work on node_modules
code.
Is there any setting?!
Expected behavior: transpile all and exec tests
Actual behavior: transpilation of node_modules code does not work, since v9
Reproduces how often: [What percentage of the time does it reproduce?]
Versions
- The output of
mocha --version
andnode node_modules/.bin/mocha --version
: - The output of
node --version
: - Your operating system
- mocha: v9.0.2
- name and version: win10
- architecture (32 or 64-bit): win 64
- Your shell (e.g., bash, zsh, PowerShell, cmd): cmder
- Your browser and version (if running browser tests): chrome
- Any third-party Mocha-related modules (and their versions):
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):
Additional Information
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module
Show activity on this post. +1 esm seems to be the easiest solution when you cannot add "type": "module" to the package. json...
Read more >TypeScript: Cannot use import statement outside a module
Another common cause of the "Cannot use import statement outside a module" error in TypeScript is trying to run a TypeScript file directly...
Read more >How to Fix "SyntaxError: Cannot use import statement outside ...
The error "SyntaxError: Cannot use import statement outside a module" is caused by the fact that the file you're trying to import is...
Read more >SyntaxError: Cannot use import statement outside a module
This error is one of the most common issue if you are trying to use ES6 features in your JavaScript project. For eg:...
Read more >How to fix "cannot use import statement outside a module"
This error occurs for one reason: you're trying to use import and you're not inside an ES module. It can happen in a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@juergba
Is it documented, that dependent modules must configure as
type: "module"
.With v8 it is not required - But with v9.
After I add
type: "module"
to the dependent @leanup/lib package.json - it works fine!THX!!!
I’ve just hit this issue too; a third party library I used before with v8 breaks in v9 with
SyntaxError: Cannot use import statement outside a module
. I’m transpiling withwebpack
in my case (or trying to!)