Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: #4625

Closed
4 tasks
khteh opened this issue Apr 10, 2021 · 4 comments
Closed
4 tasks

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: #4625

khteh opened this issue Apr 10, 2021 · 4 comments

Comments

@khteh
Copy link

khteh commented Apr 10, 2021

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • 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.
  • '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

Steps to Reproduce

run mocha <test_folder>

Expected behavior: [What you expect to happen]
No error
Actual behavior: [What actually happens]

>mocha tests
internal/modules/cjs/loader.js:1080
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Projects\Node.js\Node.JSRestAPI\tests\greetingsapi_tests.js
require() of ES modules is not supported.
require() of C:\Projects\Node.js\Node.JSRestAPI\tests\greetingsapi_tests.js from C:\Users\funcoolgeek\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename greetingsapi_tests.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Projects\Node.js\Node.JSRestAPI\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at C:\Users\funcoolgeek\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (C:\Users\funcoolgeek\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:247:14)
    at Mocha.run (C:\Users\funcoolgeek\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:576:10)
    at Object.<anonymous> (C:\Users\funcoolgeek\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:637:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_REQUIRE_ESM'
}

Reproduces how often: [What percentage of the time does it reproduce?]

Versions

5.2.0

  • The output of mocha --version and node node_modules/.bin/mocha --version: 5.2.0
  • The output of node --version:
  • Your operating system
    • name and version: Windows 10 AND container node:latest
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
  • Your browser and version (if running browser tests):
  • Any third-party Mocha-related modules (and their versions):
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): javascript. Node.JS v14.16.0

Additional Information

@juergba
Copy link
Contributor

juergba commented Apr 10, 2021

Mocha supports Node's native ESM support since v7.1.0.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Projects\Node.js\Node.JSRestAPI\tests\greetingsapi_tests.js
require() of ES modules is not supported.

Please see Node docs esm.

@juergba juergba closed this as completed Apr 10, 2021
@pr0t4zy
Copy link

pr0t4zy commented Dec 23, 2021

I have this bug on mocha@9.1.3 (node v16.13.1):

Error [ERR_REQUIRE_ESM]: require() of ES Module module.test.mjs not supported.
Instead change the require of module.test.mjs to a dynamic import() which is available in all CommonJS modules.
    at /usr/local/lib/node_modules/mocha/lib/mocha.js:430:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:427:14)
    at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:1028:10)

Doesn't it mean that Mocha tries to load the module with require()?

@juergba
Copy link
Contributor

juergba commented Dec 23, 2021

@pr0t4zy Probably yes, loadFiles() works synchronously and uses require.

In case you are using Mocha programmatically, you have to load your test files in an asynchronous way with loadFilesAsync().

@pr0t4zy
Copy link

pr0t4zy commented Dec 24, 2021

I changed:

mocha.run(failures => {
  process.exitCode = failures ? 1 : 0;
});

to

mocha.loadFilesAsync()
  .then(() => mocha.run(failures => process.exitCode = failures ? 1 : 0))
  .catch(() => process.exitCode = 1);

And works like a charm.

Thank you for the support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants