| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1123,7 +1123,26 @@ E('ERR_OUT_OF_RANGE', | |||
| 1123 | 1123 | msg += ` It must be ${range}. Received ${received}`; | |
| 1124 | 1124 | return msg; | |
| 1125 | 1125 | }, RangeError); | |
| 1126 | - E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error); | ||
| 1126 | + E('ERR_REQUIRE_ESM', | ||
| 1127 | + (filename, parentPath = null, packageJsonPath = null) => { | ||
| 1128 | + let msg = `Must use import to load ES Module: ${filename}`; | ||
| 1129 | + if (parentPath && packageJsonPath) { | ||
| 1130 | + const path = require('path'); | ||
| 1131 | + const basename = path.basename(filename) === path.basename(parentPath) ? | ||
| 1132 | + filename : path.basename(filename); | ||
| 1133 | + msg += | ||
| 1134 | + '\nrequire() of ES modules is not supported.\nrequire() of ' + | ||
| 1135 | + `${filename} ${parentPath ? `from ${parentPath} ` : ''}` + | ||
| 1136 | + 'is an ES module file as it is a .js file whose nearest parent ' + | ||
| 1137 | + 'package.json contains "type": "module" which defines all .js ' + | ||
| 1138 | + 'files in that package scope as ES modules.\nInstead rename ' + | ||
| 1139 | + `${basename} to end in .cjs, change the requiring code to use ` + | ||
| 1140 | + 'import(), or remove "type": "module" from ' + | ||
| 1141 | + `${packageJsonPath}.\n`; | ||
| 1142 | + return msg; | ||
| 1143 | + } | ||
| 1144 | + return msg; | ||
| 1145 | + }, Error); | ||
| 1127 | 1146 | E('ERR_SCRIPT_EXECUTION_INTERRUPTED', | |
| 1128 | 1147 | 'Script execution was interrupted by `SIGINT`', Error); | |
| 1129 | 1148 | E('ERR_SERVER_ALREADY_LISTEN', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1139,33 +1139,14 @@ Module.prototype._compile = function(content, filename) { | |||
| 1139 | 1139 | }; | |
| 1140 | 1140 | ||
| 1141 | 1141 | // Native extension for .js | |
| 1142 | - let warnRequireESM = true; | ||
| 1143 | 1142 | Module._extensions['.js'] = function(module, filename) { | |
| 1144 | 1143 | if (filename.endsWith('.js')) { | |
| 1145 | 1144 | const pkg = readPackageScope(filename); | |
| 1145 | + // Function require shouldn't be used in ES modules. | ||
| 1146 | 1146 | if (pkg && pkg.data && pkg.data.type === 'module') { | |
| 1147 | - if (warnRequireESM) { | ||
| 1148 | - const parentPath = module.parent && module.parent.filename; | ||
| 1149 | - const basename = parentPath && | ||
| 1150 | - path.basename(filename) === path.basename(parentPath) ? | ||
| 1151 | - filename : path.basename(filename); | ||
| 1152 | - process.emitWarning( | ||
| 1153 | - 'require() of ES modules is not supported.\nrequire() of ' + | ||
| 1154 | - `${filename} ${parentPath ? `from ${module.parent.filename} ` : ''}` + | ||
| 1155 | - 'is an ES module file as it is a .js file whose nearest parent ' + | ||
| 1156 | - 'package.json contains "type": "module" which defines all .js ' + | ||
| 1157 | - 'files in that package scope as ES modules.\nInstead rename ' + | ||
| 1158 | - `${basename} to end in .cjs, change the requiring code to use ` + | ||
| 1159 | - 'import(), or remove "type": "module" from ' + | ||
| 1160 | - `${path.resolve(pkg.path, 'package.json')}.`, | ||
| 1161 | - undefined, | ||
| 1162 | - undefined, | ||
| 1163 | - undefined, | ||
| 1164 | - true | ||
| 1165 | - ); | ||
| 1166 | - warnRequireESM = false; | ||
| 1167 | - } | ||
| 1168 | - throw new ERR_REQUIRE_ESM(filename); | ||
| 1147 | + const parentPath = module.parent && module.parent.filename; | ||
| 1148 | + const packageJsonPath = path.resolve(pkg.path, 'package.json'); | ||
| 1149 | + throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); | ||
| 1169 | 1150 | } | |
| 1170 | 1151 | } | |
| 1171 | 1152 | const content = fs.readFileSync(filename, 'utf8'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,15 +26,19 @@ child.on('close', common.mustCall((code, signal) => { | |||
| 26 | 26 | assert.strictEqual(code, 1); | |
| 27 | 27 | assert.strictEqual(signal, null); | |
| 28 | 28 | ||
| 29 | - assert.ok(stderr.startsWith(`(node:${child.pid}) Warning: ` + | ||
| 30 | - 'require() of ES modules is not supported.\nrequire() of ' + | ||
| 29 | + assert.ok(stderr.indexOf( | ||
| 30 | + `Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ${required}` + | ||
| 31 | + '\nrequire() of ES modules is not supported.\nrequire() of ' + | ||
| 31 | 32 | `${required} from ${requiring} ` + | |
| 32 | 33 | 'is an ES module file as it is a .js file whose nearest parent ' + | |
| 33 | 34 | 'package.json contains "type": "module" which defines all .js ' + | |
| 34 | 35 | 'files in that package scope as ES modules.\nInstead rename ' + | |
| 35 | 36 | `${basename} to end in .cjs, change the requiring code to use ` + | |
| 36 | 37 | 'import(), or remove "type": "module" from ' + | |
| 37 | - `${pjson}.\n`)); | ||
| 38 | + `${pjson}.\n`) !== -1); | ||
| 38 | 39 | assert.ok(stderr.indexOf( | |
| 39 | 40 | 'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module') !== -1); | |
| 41 | + | ||
| 42 | + assert.strictEqual( | ||
| 43 | + stderr.match(/Must use import to load ES Module/g).length, 1); | ||
| 40 | 44 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,10 @@ try { | |||
| 27 | 27 | require('../fixtures/es-modules/package-type-module/index.js'); | |
| 28 | 28 | assert.fail('Expected CJS to fail loading from type: module package.'); | |
| 29 | 29 | } catch (e) { | |
| 30 | - assert(e.toString().match(/Error \[ERR_REQUIRE_ESM\]: Must use import to load ES Module:/)); | ||
| 30 | + assert.strictEqual(e.name, 'Error'); | ||
| 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)); | ||
| 31 | 34 | } | |
| 32 | 35 | ||
| 33 | 36 | function expect(opt = '', inputFile, want, wantsError = false) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments