Skip to content

Commit

Permalink
further warning text adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 11, 2019
1 parent fd2f71f commit 9153981
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
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(
((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`);
}));

0 comments on commit 9153981

Please sign in to comment.