Skip to content

Commit

Permalink
esm: Add references to require() of ESM warning
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth authored and guybedford committed Oct 11, 2019
1 parent 52a5b5d commit fd2f71f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
20 changes: 14 additions & 6 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ function readPackageScope(checkPath) {
if (checkPath.endsWith(path.sep + 'node_modules'))
return false;
const pjson = readPackage(checkPath);
if (pjson) return pjson;
if (pjson) return {
path: checkPath,
data: pjson
};
}
return false;
}
Expand Down Expand Up @@ -964,13 +967,18 @@ let warnRequireESM = true;
Module._extensions['.js'] = function(module, filename) {
if (filename.endsWith('.js')) {
const pkg = readPackageScope(filename);
if (pkg && pkg.type === 'module') {
if (pkg && pkg.data && pkg.data.type === 'module') {
if (warnRequireESM) {
process.emitWarning(
'require() of .js file ' + filename + ' is not supported as it is ' +
'an ES module due to having "type": "module" in its package.json ' +
'file.\nRather use import to load this module, or if you are the ' +
'author you may want to use the .cjs extension for this file.'
((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 ` +
'import(), or remove "type": "module" from ' +
`${path.resolve(pkg.path, 'package.json')}.`
);
warnRequireESM = false;
}
Expand Down
22 changes: 15 additions & 7 deletions test/es-module/test-cjs-esm-warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const { spawn } = require('child_process');
const assert = require('assert');
const path = require('path');

const entry = fixtures.path('/es-modules/cjs-esm.js');
const requiring = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
const required = path.resolve(
fixtures.path('/es-modules/package-type-module/cjs.js')
);
const pjson = path.resolve(
fixtures.path('/es-modules/package-type-module/package.json')
);

const child = spawn(process.execPath, [entry]);
const child = spawn(process.execPath, [requiring]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
Expand All @@ -17,9 +23,11 @@ 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: require() of .js ` +
`file ${path.resolve(entry, '../package-type-module/cjs.js')} is not ` +
'supported as it is an ES module due to having "type": "module" in its ' +
'package.json file.\nRather use import to load this module, or if you ' +
'are the author you may want to use the .cjs extension for this file.\n');
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`);
}));

0 comments on commit fd2f71f

Please sign in to comment.