Skip to content

Commit be7a0c5

Browse files
authored
module: do not warn for typeless package.json when there isn't one
It was intended that warnings should only be emitted for an existing package.json without a type. This fixes a confusing warning telling users to update /package.json when there are no package.json on the lookup path at all, like this: [MODULE_TYPELESS_PACKAGE_JSON] Warning: ... parsed as an ES module because module syntax was detected; to avoid the performance penalty of syntax detection, add "type": "module" to /package.json Drive-by: update the warning message to be clear about reparsing and make it clear what's actionable. PR-URL: #54045 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 269175b commit be7a0c5

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/internal/modules/esm/get_format.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ let typelessPackageJsonFilesWarnedAbout;
9292
function warnTypelessPackageJsonFile(pjsonPath, url) {
9393
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
9494
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
95-
const warning = `${url} parsed as an ES module because module syntax was detected;` +
96-
` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`;
95+
const warning = `Module type of ${url} is not specified and it doesn't parse as CommonJS.\n` +
96+
'Reparsing as ES module because module syntax was detected. This incurs a performance overhead.\n' +
97+
`To eliminate this warning, add "type": "module" to ${pjsonPath}.`;
9798
process.emitWarning(warning, {
9899
code: 'MODULE_TYPELESS_PACKAGE_JSON',
99100
});
@@ -112,7 +113,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112113
const ext = extname(url);
113114

114115
if (ext === '.js') {
115-
const { type: packageType, pjsonPath } = getPackageScopeConfig(url);
116+
const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url);
116117
if (packageType !== 'none') {
117118
return packageType;
118119
}
@@ -133,7 +134,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
133134
// For ambiguous files (no type field, .js extension) we return
134135
// undefined from `resolve` and re-run the check in `load`.
135136
const format = detectModuleFormat(source, url);
136-
if (format === 'module') {
137+
if (format === 'module' && foundPackageJson) {
137138
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
138139
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
139140
warnTypelessPackageJsonFile(pjsonPath, url);
@@ -143,7 +144,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
143144
}
144145
}
145146
if (ext === '.ts' && getOptionValue('--experimental-strip-types')) {
146-
const { type: packageType, pjsonPath } = getPackageScopeConfig(url);
147+
const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url);
147148
if (packageType !== 'none') {
148149
return `${packageType}-typescript`;
149150
}
@@ -164,7 +165,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
164165
const parsedSource = tsParse(source);
165166
const detectedFormat = detectModuleFormat(parsedSource, url);
166167
const format = detectedFormat ? `${detectedFormat}-typescript` : 'commonjs-typescript';
167-
if (format === 'module-typescript') {
168+
if (format === 'module-typescript' && foundPackageJson) {
168169
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
169170
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
170171
warnTypelessPackageJsonFile(pjsonPath, url);

test/es-module/test-esm-detect-ambiguous.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,18 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
352352
});
353353
}
354354

355+
it('does not warn when there are no package.json', async () => {
356+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
357+
fixtures.path('es-modules/loose.js'),
358+
]);
359+
360+
strictEqual(stderr, '');
361+
strictEqual(stdout, 'executed\n');
362+
strictEqual(code, 0);
363+
strictEqual(signal, null);
364+
});
365+
366+
355367
it('warns only once for a package.json that affects multiple files', async () => {
356368
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
357369
fixtures.path('es-modules/package-without-type/detected-as-esm.js'),

0 commit comments

Comments
 (0)