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