From 7673ee4b3f534a987e3b601398871f38b7d75d59 Mon Sep 17 00:00:00 2001 From: Daniel Compton Date: Fri, 15 Sep 2023 17:13:54 +1200 Subject: [PATCH] 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. Refs: https://github.com/nodejs/node/pull/39175 PR-URL: https://github.com/nodejs/node/pull/49521 Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Jacob Smith Reviewed-By: Geoffrey Booth Reviewed-By: Stephen Belanger --- lib/internal/errors.js | 2 +- test/es-module/test-cjs-esm-warn.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 28c241f6fedda9..ab5f84e5d720a6 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1542,7 +1542,7 @@ E('ERR_REQUIRE_ESM', msg += `\n${basename} 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.' + - `\nInstead rename ${basename} to end in .cjs, change the requiring ` + + `\nInstead either rename ${basename} 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 ' + `${packageJsonPath} to treat all .js files as CommonJS (using .mjs for ` + diff --git a/test/es-module/test-cjs-esm-warn.js b/test/es-module/test-cjs-esm-warn.js index c1d60a209502bb..7ac85fd58c5f18 100644 --- a/test/es-module/test-cjs-esm-warn.js +++ b/test/es-module/test-cjs-esm-warn.js @@ -31,7 +31,7 @@ describe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => { ); assert.ok( stderr.replaceAll('\r', '').includes( - `Instead rename ${basename} to end in .cjs, change the requiring ` + + `Instead either rename ${basename} 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 ${pjson} to ` + 'treat all .js files as CommonJS (using .mjs for all ES modules ' +