-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: warn on require of .js inside type: module
PR-URL: #29909 Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
1fefd7f
commit 2695f82
Showing
4 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const { spawn } = require('child_process'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
|
||
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 basename = 'cjs.js'; | ||
|
||
const child = spawn(process.execPath, [requiring]); | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += 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 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`); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./package-type-module/cjs.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'asdf'; |