Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
further warning text adjustments
  • Loading branch information
guybedford committed Oct 11, 2019
commit 9153981ab54341d1eac75edd59757951b3f9ff1e
17 changes: 10 additions & 7 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Comment thread
guybedford marked this conversation as resolved.
Outdated
((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')}.`
);
Expand Down
19 changes: 12 additions & 7 deletions test/es-module/test-cjs-esm-warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`);
}));