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.

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

See original GitHub issue

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)

Affected packages and versions: latest

Steps to reproduce

Install in Node 10 environment (I haven’t tested on other node versions). Receive the following error:

Link to code example: https://github.com/googleapis/release-please/pull/870/checks?check_run_id=2390814891

Expected behavior

Library compiles.

I believe the goal is perhaps a dual-mode module (that would work for ESM and CJS?). This might just take some fiddling with extensions and export maps.

Here’s what we landed on for yargs:

https://github.com/yargs/yargs/blob/master/package.json#L5

It’s ugly but works on most Node versions.

Actual behavior

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/runner/work/release-please/release-please/node_modules/unist-util-visit/index.js
require() of ES modules is not supported.
require() of /home/runner/work/release-please/release-please/node_modules/unist-util-visit/index.js from /home/runner/work/release-please/release-please/build/src/util/to-conventional-changelog-format.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/runner/work/release-please/release-please/node_modules/unist-util-visit/package.json.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
wooormcommented, Jun 11, 2021
  • see the linked posts for more on this discussion and Next
  • my experience maintaining dual esm/cjs is that there are tons of problems
  • feel free to use the previous version while you or your tools are not ready
1reaction
johnromcommented, Nov 18, 2021

Rollup is a bundler made for ESM. I don’t see why workarounds are needed to support ESM?

Workaround isn’t needed to support ESM, it’s used to support CJS consumers of a library built with rollup (or any bundler) (assuming they still exist out there in the world). I ran into this issue with an older Gatsby version that is otherwise working for now, for example.

Without this workaround, the cjs bundle will attempt to require the esm module upstream and receive this error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Must use import to load ES Module · Issue #28 · sindresorhus ...
In Node.js v14, I got this error. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/salty/Documents/ ...
Read more >
Error [ERR_REQUIRE_ESM]: How to use es6 modules in ...
All you have to do is adding the flag --experimental-modules that supports the new es6 import/export statement also the order is important ...
Read more >
Error [ERR_REQUIRE_ESM]: Must use import to load ES ...
The error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module occurs for 2 main reasons a module you are importing has been...
Read more >
Error ERR REQUIRE ESM | Must use import to load ES Module
Error ERR REQUIRE ESM | Must use import to load ES Module. 15K views 1 year ago JS - JavaScript · Sagar S....
Read more >
Must use import to load ES Module - Netlify Support Forums
Hello, I have been getting this error on production, but it works fine in my local when I try to preview using netlify...
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