GeezerVison is showing github.com — esm: refine ERR_REQUIRE_ESM errors · jigfox/node@e2a6399
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +56-15Lines changed: 56 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ const { | 14 | 14 | AggregateError, | 15 | 15 | ArrayFrom, | 16 | 16 | ArrayIsArray, | | 17 | + ArrayPrototypeFilter, | 17 | 18 | ArrayPrototypeIncludes, | 18 | 19 | ArrayPrototypeIndexOf, | 19 | 20 | ArrayPrototypeJoin, | @@ -788,6 +789,34 @@ const fatalExceptionStackEnhancers = { | 788 | 789 | } | 789 | 790 | }; | 790 | 791 |
| | 792 | +// Ensures the printed error line is from user code. | | 793 | +let _kArrowMessagePrivateSymbol, _setHiddenValue; | | 794 | +function setArrowMessage(err, arrowMessage) { | | 795 | + if (!_kArrowMessagePrivateSymbol) { | | 796 | + ({ | | 797 | + arrow_message_private_symbol: _kArrowMessagePrivateSymbol, | | 798 | + setHiddenValue: _setHiddenValue, | | 799 | + } = internalBinding('util')); | | 800 | + } | | 801 | + _setHiddenValue(err, _kArrowMessagePrivateSymbol, arrowMessage); | | 802 | +} | | 803 | + | | 804 | +// Hide stack lines before the first user code line. | | 805 | +function hideInternalStackFrames(error) { | | 806 | + overrideStackTrace.set(error, (error, stackFrames) => { | | 807 | + let frames = stackFrames; | | 808 | + if (typeof stackFrames === 'object') { | | 809 | + frames = ArrayPrototypeFilter( | | 810 | + stackFrames, | | 811 | + (frm) => !StringPrototypeStartsWith(frm.getFileName(), | | 812 | + 'node:internal') | | 813 | + ); | | 814 | + } | | 815 | + ArrayPrototypeUnshift(frames, error); | | 816 | + return ArrayPrototypeJoin(frames, '\n at '); | | 817 | + }); | | 818 | +} | | 819 | + | 791 | 820 | // Node uses an AbortError that isn't exactly the same as the DOMException | 792 | 821 | // to make usage of the error in userland and readable-stream easier. | 793 | 822 | // It is a regular error with `.code` and `.name`. | @@ -806,8 +835,10 @@ module.exports = { | 806 | 835 | exceptionWithHostPort, | 807 | 836 | getMessage, | 808 | 837 | hideStackFrames, | | 838 | + hideInternalStackFrames, | 809 | 839 | isErrorStackTraceLimitWritable, | 810 | 840 | isStackOverflowError, | | 841 | + setArrowMessage, | 811 | 842 | connResetException, | 812 | 843 | uvErrmapGet, | 813 | 844 | uvException, | @@ -842,6 +873,7 @@ module.exports = { | 842 | 873 | // Note: Please try to keep these in alphabetical order | 843 | 874 | // | 844 | 875 | // Note: Node.js specific errors must begin with the prefix ERR_ | | 876 | + | 845 | 877 | E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError); | 846 | 878 | E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError); | 847 | 879 | E('ERR_ASSERTION', '%s', Error); | @@ -1406,23 +1438,32 @@ E('ERR_PERFORMANCE_INVALID_TIMESTAMP', | 1406 | 1438 | '%d is not a valid timestamp', TypeError); | 1407 | 1439 | E('ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS', '%s', TypeError); | 1408 | 1440 | E('ERR_REQUIRE_ESM', | 1409 | | - (filename, parentPath = null, packageJsonPath = null) => { | 1410 | | - let msg = `Must use import to load ES Module: ${filename}`; | 1411 | | - if (parentPath && packageJsonPath) { | 1412 | | - const path = require('path'); | 1413 | | - const basename = path.basename(filename) === path.basename(parentPath) ? | 1414 | | - filename : path.basename(filename); | 1415 | | - msg += | 1416 | | - '\nrequire() of ES modules is not supported.\nrequire() of ' + | 1417 | | - `${filename} from ${parentPath} ` + | 1418 | | - 'is an ES module file as it is a .js file whose nearest parent ' + | 1419 | | - 'package.json contains "type": "module" which defines all .js ' + | 1420 | | - 'files in that package scope as ES modules.\nInstead rename ' + | 1421 | | - `${basename} to end in .cjs, change the requiring code to use ` + | 1422 | | - 'import(), or remove "type": "module" from ' + | 1423 | | - `${packageJsonPath}.\n`; | | 1441 | + function(filename, hasEsmSyntax, parentPath = null, packageJsonPath = null) { | | 1442 | + hideInternalStackFrames(this); | | 1443 | + let msg = `require() of ES Module ${filename}${parentPath ? ` from ${ | | 1444 | + parentPath}` : ''} not supported.`; | | 1445 | + if (!packageJsonPath) { | | 1446 | + if (StringPrototypeEndsWith(filename, '.mjs')) | | 1447 | + msg += `\nInstead change the require of ${filename} to a dynamic ` + | | 1448 | + 'import() which is available in all CommonJS modules.'; | | 1449 | + return msg; | | 1450 | + } | | 1451 | + const path = require('path'); | | 1452 | + const basename = path.basename(filename) === path.basename(parentPath) ? | | 1453 | + filename : path.basename(filename); | | 1454 | + if (hasEsmSyntax) { | | 1455 | + msg += `\nInstead change the require of ${basename} in ${parentPath} to` + | | 1456 | + ' a dynamic import() which is available in all CommonJS modules.'; | 1424 | 1457 | return msg; | 1425 | 1458 | } | | 1459 | + msg += `\n${basename} is treated as an ES module file as it is a .js ` + | | 1460 | + 'file whose nearest parent package.json contains "type": "module" ' + | | 1461 | + 'which declares all .js files in that package scope as ES modules.' + | | 1462 | + `\nInstead rename ${basename} to end in .cjs, change the requiring ` + | | 1463 | + 'code to use dynamic import() which is available in all CommonJS ' + | | 1464 | + 'modules, or change "type": "module" to "type": "commonjs" in ' + | | 1465 | + `${packageJsonPath} to treat all .js files as CommonJS (using .mjs for ` + | | 1466 | + 'all ES modules instead).\n'; | 1426 | 1467 | return msg; | 1427 | 1468 | }, Error); | 1428 | 1469 | E('ERR_SCRIPT_EXECUTION_INTERRUPTED', | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/helpers.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | ArrayPrototypeForEach, | 5 | 5 | ArrayPrototypeJoin, | | 6 | + ArrayPrototypeSome, | 6 | 7 | ObjectDefineProperty, | 7 | 8 | ObjectPrototypeHasOwnProperty, | 8 | 9 | SafeMap, | @@ -184,9 +185,26 @@ function normalizeReferrerURL(referrer) { | 184 | 185 | return new URL(referrer).href; | 185 | 186 | } | 186 | 187 |
| | 188 | +// For error messages only - used to check if ESM syntax is in use. | | 189 | +function hasEsmSyntax(code) { | | 190 | + const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser; | | 191 | + let root; | | 192 | + try { | | 193 | + root = parser.parse(code, { sourceType: 'module', ecmaVersion: 'latest' }); | | 194 | + } catch { | | 195 | + return false; | | 196 | + } | | 197 | + | | 198 | + return ArrayPrototypeSome(root.body, (stmt) => | | 199 | + stmt.type === 'ExportNamedDeclaration' || | | 200 | + stmt.type === 'ImportDeclaration' || | | 201 | + stmt.type === 'ExportAllDeclaration'); | | 202 | +} | | 203 | + | 187 | 204 | module.exports = { | 188 | 205 | addBuiltinLibsToObject, | 189 | 206 | cjsConditions, | | 207 | + hasEsmSyntax, | 190 | 208 | loadNativeModule, | 191 | 209 | makeRequireFunction, | 192 | 210 | normalizeReferrerURL, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js +47-18Lines changed: 47 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ const { | 48 | 48 | Proxy, | 49 | 49 | ReflectApply, | 50 | 50 | ReflectSet, | | 51 | + RegExpPrototypeExec, | 51 | 52 | RegExpPrototypeTest, | 52 | 53 | SafeMap, | 53 | 54 | SafeWeakMap, | @@ -58,6 +59,7 @@ const { | 58 | 59 | StringPrototypeLastIndexOf, | 59 | 60 | StringPrototypeIndexOf, | 60 | 61 | StringPrototypeMatch, | | 62 | + StringPrototypeRepeat, | 61 | 63 | StringPrototypeSlice, | 62 | 64 | StringPrototypeSplit, | 63 | 65 | StringPrototypeStartsWith, | @@ -88,11 +90,12 @@ const { internalModuleStat } = internalBinding('fs'); | 88 | 90 | const packageJsonReader = require('internal/modules/package_json_reader'); | 89 | 91 | const { safeGetenv } = internalBinding('credentials'); | 90 | 92 | const { | | 93 | + cjsConditions, | | 94 | + hasEsmSyntax, | | 95 | + loadNativeModule, | 91 | 96 | makeRequireFunction, | 92 | 97 | normalizeReferrerURL, | 93 | 98 | stripBOM, | 94 | | - cjsConditions, | 95 | | - loadNativeModule | 96 | 99 | } = require('internal/modules/cjs/helpers'); | 97 | 100 | const { getOptionValue } = require('internal/options'); | 98 | 101 | const preserveSymlinks = getOptionValue('--preserve-symlinks'); | @@ -107,11 +110,14 @@ const policy = getOptionValue('--experimental-policy') ? | 107 | 110 | let hasLoadedAnyUserCJSModule = false; | 108 | 111 |
| 109 | 112 | const { | 110 | | - ERR_INVALID_ARG_VALUE, | 111 | | - ERR_INVALID_MODULE_SPECIFIER, | 112 | | - ERR_REQUIRE_ESM, | 113 | | - ERR_UNKNOWN_BUILTIN_MODULE, | 114 | | -} = require('internal/errors').codes; | | 113 | + codes: { | | 114 | + ERR_INVALID_ARG_VALUE, | | 115 | + ERR_INVALID_MODULE_SPECIFIER, | | 116 | + ERR_REQUIRE_ESM, | | 117 | + ERR_UNKNOWN_BUILTIN_MODULE, | | 118 | + }, | | 119 | + setArrowMessage, | | 120 | +} = require('internal/errors'); | 115 | 121 | const { validateString } = require('internal/validators'); | 116 | 122 | const pendingDeprecation = getOptionValue('--pending-deprecation'); | 117 | 123 |
| @@ -970,7 +976,7 @@ Module.prototype.load = function(filename) { | 970 | 976 | const extension = findLongestRegisteredExtension(filename); | 971 | 977 | // allow .mjs to be overridden | 972 | 978 | if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs']) | 973 | | - throw new ERR_REQUIRE_ESM(filename); | | 979 | + throw new ERR_REQUIRE_ESM(filename, true); | 974 | 980 |
| 975 | 981 | Module._extensions[extension](this, filename); | 976 | 982 | this.loaded = true; | @@ -1102,16 +1108,6 @@ Module.prototype._compile = function(content, filename) { | 1102 | 1108 |
| 1103 | 1109 | // Native extension for .js | 1104 | 1110 | Module._extensions['.js'] = function(module, filename) { | 1105 | | - if (StringPrototypeEndsWith(filename, '.js')) { | 1106 | | - const pkg = readPackageScope(filename); | 1107 | | - // Function require shouldn't be used in ES modules. | 1108 | | - if (pkg?.data?.type === 'module') { | 1109 | | - const parent = moduleParentCache.get(module); | 1110 | | - const parentPath = parent?.filename; | 1111 | | - const packageJsonPath = path.resolve(pkg.path, 'package.json'); | 1112 | | - throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); | 1113 | | - } | 1114 | | - } | 1115 | 1111 | // If already analyzed the source, then it will be cached. | 1116 | 1112 | const cached = cjsParseCache.get(module); | 1117 | 1113 | let content; | @@ -1121,6 +1117,39 @@ Module._extensions['.js'] = function(module, filename) { | 1121 | 1117 | } else { | 1122 | 1118 | content = fs.readFileSync(filename, 'utf8'); | 1123 | 1119 | } | | 1120 | + if (StringPrototypeEndsWith(filename, '.js')) { | | 1121 | + const pkg = readPackageScope(filename); | | 1122 | + // Function require shouldn't be used in ES modules. | | 1123 | + if (pkg?.data?.type === 'module') { | | 1124 | + const parent = moduleParentCache.get(module); | | 1125 | + const parentPath = parent?.filename; | | 1126 | + const packageJsonPath = path.resolve(pkg.path, 'package.json'); | | 1127 | + const usesEsm = hasEsmSyntax(content); | | 1128 | + const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath, | | 1129 | + packageJsonPath); | | 1130 | + // Attempt to reconstruct the parent require frame. | | 1131 | + if (Module._cache[parentPath]) { | | 1132 | + let parentSource; | | 1133 | + try { | | 1134 | + parentSource = fs.readFileSync(parentPath, 'utf8'); | | 1135 | + } catch {} | | 1136 | + if (parentSource) { | | 1137 | + const errLine = StringPrototypeSplit( | | 1138 | + StringPrototypeSlice(err.stack, StringPrototypeIndexOf( | | 1139 | + err.stack, ' at ')), '\n', 1)[0]; | | 1140 | + const { 1: line, 2: col } = | | 1141 | + RegExpPrototypeExec(/(\d+):(\d+)\)/, errLine) || []; | | 1142 | + if (line && col) { | | 1143 | + const srcLine = StringPrototypeSplit(parentSource, '\n')[line - 1]; | | 1144 | + const frame = `${parentPath}:${line}\n${srcLine}\n${ | | 1145 | + StringPrototypeRepeat(' ', col - 1)}^\n`; | | 1146 | + setArrowMessage(err, frame); | | 1147 | + } | | 1148 | + } | | 1149 | + } | | 1150 | + throw err; | | 1151 | + } | | 1152 | + } | 1124 | 1153 | module._compile(content, filename); | 1125 | 1154 | }; | 1126 | 1155 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.cc +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,6 +215,12 @@ void AppendExceptionLine(Environment* env, | 215 | 215 | Local<Object> err_obj; | 216 | 216 | if (!er.IsEmpty() && er->IsObject()) { | 217 | 217 | err_obj = er.As<Object>(); | | 218 | + // If arrow_message is already set, skip. | | 219 | + auto maybe_value = err_obj->GetPrivate(env->context(), | | 220 | + env->arrow_message_private_symbol()); | | 221 | + Local<Value> lvalue; | | 222 | + if (!maybe_value.ToLocal(&lvalue) || lvalue->IsString()) | | 223 | + return; | 218 | 224 | } | 219 | 225 |
| 220 | 226 | bool added_exception_line = false; | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-cjs-esm-warn.js +50-27Lines changed: 50 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,36 +6,59 @@ const { spawn } = require('child_process'); | 6 | 6 | const assert = require('assert'); | 7 | 7 | const path = require('path'); | 8 | 8 |
| 9 | | -const requiring = path.resolve(fixtures.path('/es-modules/cjs-esm.js')); | 10 | | -const required = path.resolve( | 11 | | - fixtures.path('/es-modules/package-type-module/cjs.js') | 12 | | -); | | 9 | +const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js')); | | 10 | +const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js')); | 13 | 11 | const pjson = path.resolve( | 14 | 12 | fixtures.path('/es-modules/package-type-module/package.json') | 15 | 13 | ); | 16 | 14 |
| 17 | | -const basename = 'cjs.js'; | | 15 | +{ | | 16 | + const required = path.resolve( | | 17 | + fixtures.path('/es-modules/package-type-module/cjs.js') | | 18 | + ); | | 19 | + const basename = 'cjs.js'; | | 20 | + const child = spawn(process.execPath, [requiringCjsAsEsm]); | | 21 | + let stderr = ''; | | 22 | + child.stderr.setEncoding('utf8'); | | 23 | + child.stderr.on('data', (data) => { | | 24 | + stderr += data; | | 25 | + }); | | 26 | + child.on('close', common.mustCall((code, signal) => { | | 27 | + assert.strictEqual(code, 1); | | 28 | + assert.strictEqual(signal, null); | | 29 | + | | 30 | + assert.ok(stderr.replaceAll('\r', '').includes( | | 31 | + `Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ | | 32 | + requiringCjsAsEsm} not supported.\n`)); | | 33 | + assert.ok(stderr.replaceAll('\r', '').includes( | | 34 | + `Instead rename ${basename} to end in .cjs, change the requiring ` + | | 35 | + 'code to use dynamic import() which is available in all CommonJS ' + | | 36 | + `modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` + | | 37 | + 'treat all .js files as CommonJS (using .mjs for all ES modules ' + | | 38 | + 'instead).\n')); | | 39 | + })); | | 40 | +} | 18 | 41 |
| 19 | | -const child = spawn(process.execPath, [requiring]); | 20 | | -let stderr = ''; | 21 | | -child.stderr.setEncoding('utf8'); | 22 | | -child.stderr.on('data', (data) => { | 23 | | - stderr += data; | 24 | | -}); | 25 | | -child.on('close', common.mustCall((code, signal) => { | 26 | | - assert.strictEqual(code, 1); | 27 | | - assert.strictEqual(signal, null); | | 42 | +{ | | 43 | + const required = path.resolve( | | 44 | + fixtures.path('/es-modules/package-type-module/esm.js') | | 45 | + ); | | 46 | + const basename = 'esm.js'; | | 47 | + const child = spawn(process.execPath, [requiringEsm]); | | 48 | + let stderr = ''; | | 49 | + child.stderr.setEncoding('utf8'); | | 50 | + child.stderr.on('data', (data) => { | | 51 | + stderr += data; | | 52 | + }); | | 53 | + child.on('close', common.mustCall((code, signal) => { | | 54 | + assert.strictEqual(code, 1); | | 55 | + assert.strictEqual(signal, null); | 28 | 56 |
| 29 | | - assert.ok(stderr.replace(/\r/g, '').includes( | 30 | | - `Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ${required}` + | 31 | | - '\nrequire() of ES modules is not supported.\nrequire() of ' + | 32 | | - `${required} from ${requiring} ` + | 33 | | - 'is an ES module file as it is a .js file whose nearest parent ' + | 34 | | - 'package.json contains "type": "module" which defines all .js ' + | 35 | | - 'files in that package scope as ES modules.\nInstead rename ' + | 36 | | - `${basename} to end in .cjs, change the requiring code to use ` + | 37 | | - 'import(), or remove "type": "module" from ' + | 38 | | - `${pjson}.\n`)); | 39 | | - assert.ok(stderr.includes( | 40 | | - 'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module')); | 41 | | -})); | | 57 | + assert.ok(stderr.replace(/\r/g, '').includes( | | 58 | + `Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ | | 59 | + requiringEsm} not supported.\n`)); | | 60 | + assert.ok(stderr.replace(/\r/g, '').includes( | | 61 | + `Instead change the require of ${basename} in ${requiringEsm} to` + | | 62 | + ' a dynamic import() which is available in all CommonJS modules.\n')); | | 63 | + })); | | 64 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-type-flag-errors.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 29 | 29 | } catch (e) { | 30 | 30 | assert.strictEqual(e.name, 'Error'); | 31 | 31 | assert.strictEqual(e.code, 'ERR_REQUIRE_ESM'); | 32 | | - assert(e.toString().match(/Must use import to load ES Module/g)); | 33 | | - assert(e.message.match(/Must use import to load ES Module/g)); | | 32 | + assert(e.toString().match(/require\(\) of ES Module/g)); | | 33 | + assert(e.message.match(/require\(\) of ES Module/g)); | 34 | 34 | } | 35 | 35 |
| 36 | 36 | function expect(opt = '', inputFile, want, wantsError = false) { | |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +require('./package-type-module/esm.js'); |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export var p = 5; |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-require-mjs.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,7 @@ const assert = require('assert'); | 5 | 5 | assert.throws( | 6 | 6 | () => require('../fixtures/es-modules/test-esm-ok.mjs'), | 7 | 7 | { | 8 | | - message: /Must use import to load ES Module/, | | 8 | + message: /dynamic import\(\) which is available in all CommonJS modules/, | 9 | 9 | code: 'ERR_REQUIRE_ESM' | 10 | 10 | } | 11 | 11 | ); |
|