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.

ESM problem `Cannot use import statement outside a module`

See original GitHub issue

Prerequisites

  • [ 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) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

image

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 and node 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:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
deleoniocommented, Jul 20, 2021

@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!!!

0reactions
mattgodboltcommented, Sep 28, 2021

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 with webpack in my case (or trying to!)

Read more comments on GitHub >

github_iconTop 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 >

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