diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 22e8103517b979..d7e240fbea5761 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -969,14 +969,17 @@ Module._extensions['.js'] = function(module, filename) { const pkg = readPackageScope(filename); if (pkg && pkg.data && pkg.data.type === 'module') { if (warnRequireESM) { + const parentPath = module.parent && module.parent.filename; + const basename = parentPath && + path.basename(filename) === path.basename(parentPath) ? + filename : path.basename(filename); process.emitWarning( - ((module.parent && module.parent.filename) ? - `${module.parent.filename} contains a ` : '') + - `require() of ${filename}, which is a .js file whose nearest ` + - 'parent package.json contains "type": "module" which therefore ' + - 'defines all .js files in that package scope as ES modules. ' + - 'require() of ES modules is not allowed. Instead, rename ' + - `${filename} to end in .cjs, or change the requiring code to use ` + + 'require() of ES modules is not supported.\nrequire() of ' + + `${filename} ${parentPath ? `from ${module.parent.filename} ` : ''}` + + 'is an ES module file as it is a .js file whose nearest parent ' + + 'package.json contains "type": "module" which defines all .js ' + + 'files in that package scope as ES modules.\nInstead rename ' + + `${basename} to end in .cjs, change the requiring code to use ` + 'import(), or remove "type": "module" from ' + `${path.resolve(pkg.path, 'package.json')}.` ); diff --git a/test/es-module/test-cjs-esm-warn.js b/test/es-module/test-cjs-esm-warn.js index 238c1c48c36358..ec368c73e2ef2d 100644 --- a/test/es-module/test-cjs-esm-warn.js +++ b/test/es-module/test-cjs-esm-warn.js @@ -14,6 +14,8 @@ const pjson = path.resolve( fixtures.path('/es-modules/package-type-module/package.json') ); +const basename = 'cjs.js'; + const child = spawn(process.execPath, [requiring]); let stderr = ''; child.stderr.setEncoding('utf8'); @@ -23,11 +25,14 @@ child.stderr.on('data', (data) => { child.on('close', common.mustCall((code, signal) => { assert.strictEqual(code, 0); assert.strictEqual(signal, null); - assert.strictEqual(stderr, `(node:${child.pid}) Warning: ${requiring} ` + - `contains a require() of ${required}, which is a .js file whose nearest ` + - 'parent package.json contains "type": "module" which therefore defines ' + - 'all .js files in that package scope as ES modules. require() of ES ' + - `modules is not allowed. Instead, rename ${required} to end in .cjs, or ` + - 'change the requiring code to use import(), or remove "type": "module" ' + - `from ${pjson}.\n`); + + assert.strictEqual(stderr, `(node:${child.pid}) Warning: ` + + 'require() of ES modules is not supported.\nrequire() of ' + + `${required} from ${requiring} ` + + 'is an ES module file as it is a .js file whose nearest parent ' + + 'package.json contains "type": "module" which defines all .js ' + + 'files in that package scope as ES modules.\nInstead rename ' + + `${basename} to end in .cjs, change the requiring code to use ` + + 'import(), or remove "type": "module" from ' + + `${pjson}.\n`); }));