Skip to content

esm: clarify ERR_REQUIRE_ESM errors

In #39175, better ESM errors were introduced. This commit tweaks the language in the error slightly to make it clear that there are three different options to resolve the error.

I find the current message more difficult to parse, because you only realise that you need to pick one of the three options after reading most of the (very long) sentence. Putting "either" at the start of the message should signpost that these are three options and you can pick any of them.

Before

[Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/me/Developer/company/repo/app/src/tracer.js not supported.
tracer.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename tracer.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/me/Developer/company/repo/app/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

After

[Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/me/Developer/company/repo/app/src/tracer.js not supported.
tracer.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename tracer.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/me/Developer/company/repo/app/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Merge request reports

Loading