Skip to content

Commit a3a9f40

Browse files
committed
Do not error on require(esm) in node20/nodenext
1 parent 4fba3aa commit a3a9f40

File tree

40 files changed

+73
-922
lines changed

40 files changed

+73
-922
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4714,7 +4714,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47144714
if (errorNode && resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
47154715
errorOnImplicitAnyModule(/*isError*/ false, errorNode, currentSourceFile, mode, resolvedModule, moduleReference);
47164716
}
4717-
if (errorNode && (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext)) {
4717+
if (errorNode && (moduleKind === ModuleKind.Node16 || moduleKind === ModuleKind.Node18)) {
47184718
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
47194719
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l));
47204720
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of

tests/baselines/reference/nodeAllowJsPackageSelfName(module=node20).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
2-
index.cjs(2,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
32

43

54
!!! error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
@@ -11,11 +10,9 @@ index.cjs(2,23): error TS1479: The current file is a CommonJS module whose impor
1110
// esm format file
1211
import * as self from "package";
1312
self;
14-
==== index.cjs (1 errors) ====
13+
==== index.cjs (0 errors) ====
1514
// esm format file
1615
import * as self from "package";
17-
~~~~~~~~~
18-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
1916
self;
2017
==== package.json (0 errors) ====
2118
{

tests/baselines/reference/nodeAllowJsPackageSelfName(module=nodenext).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
2-
index.cjs(2,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
32

43

54
!!! error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
@@ -11,11 +10,9 @@ index.cjs(2,23): error TS1479: The current file is a CommonJS module whose impor
1110
// esm format file
1211
import * as self from "package";
1312
self;
14-
==== index.cjs (1 errors) ====
13+
==== index.cjs (0 errors) ====
1514
// esm format file
1615
import * as self from "package";
17-
~~~~~~~~~
18-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
1916
self;
2017
==== package.json (0 errors) ====
2118
{

tests/baselines/reference/nodeModules1(module=node20).errors.txt

Lines changed: 3 additions & 81 deletions
Large diffs are not rendered by default.

tests/baselines/reference/nodeModules1(module=nodenext).errors.txt

Lines changed: 3 additions & 81 deletions
Large diffs are not rendered by default.

tests/baselines/reference/nodeModulesAllowJs1(module=node20).errors.txt

Lines changed: 3 additions & 81 deletions
Large diffs are not rendered by default.

tests/baselines/reference/nodeModulesAllowJs1(module=nodenext).errors.txt

Lines changed: 3 additions & 81 deletions
Large diffs are not rendered by default.

tests/baselines/reference/nodeModulesAllowJsConditionalPackageExports(module=node20).errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
2-
index.cjs(3,22): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
3-
index.cjs(4,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
42
node_modules/inner/index.d.mts(2,13): error TS2303: Circular definition of import alias 'cjs'.
53
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.
64

@@ -38,15 +36,11 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
3836
mjsi.mjsSource;
3937
typei.mjsSource;
4038
ts.mjsSource;
41-
==== index.cjs (2 errors) ====
39+
==== index.cjs (0 errors) ====
4240
// cjs format file
4341
import * as cjs from "package/cjs";
4442
import * as mjs from "package/mjs";
45-
~~~~~~~~~~~~~
46-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
4743
import * as type from "package";
48-
~~~~~~~~~
49-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
5044
cjs;
5145
mjs;
5246
type;

tests/baselines/reference/nodeModulesAllowJsConditionalPackageExports(module=nodenext).errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
2-
index.cjs(3,22): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
3-
index.cjs(4,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
42
node_modules/inner/index.d.mts(2,13): error TS2303: Circular definition of import alias 'cjs'.
53
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.
64

@@ -38,15 +36,11 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
3836
mjsi.mjsSource;
3937
typei.mjsSource;
4038
ts.mjsSource;
41-
==== index.cjs (2 errors) ====
39+
==== index.cjs (0 errors) ====
4240
// cjs format file
4341
import * as cjs from "package/cjs";
4442
import * as mjs from "package/mjs";
45-
~~~~~~~~~~~~~
46-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
4743
import * as type from "package";
48-
~~~~~~~~~
49-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
5044
cjs;
5145
mjs;
5246
type;

tests/baselines/reference/nodeModulesAllowJsPackageExports(module=node20).errors.txt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
2-
index.cjs(3,22): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
3-
index.cjs(4,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
4-
index.cjs(9,23): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
5-
node_modules/inner/index.d.cts(3,22): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
62
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.
7-
node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
83

94

105
!!! error TS2209: The project root is ambiguous, but is required to resolve export map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
@@ -36,34 +31,26 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
3631
cjsi;
3732
mjsi;
3833
typei;
39-
==== index.cjs (3 errors) ====
34+
==== index.cjs (0 errors) ====
4035
// cjs format file
4136
import * as cjs from "package/cjs";
4237
import * as mjs from "package/mjs";
43-
~~~~~~~~~~~~~
44-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package/mjs")' call instead.
4538
import * as type from "package";
46-
~~~~~~~~~
47-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("package")' call instead.
4839
cjs;
4940
mjs;
5041
type;
5142
import * as cjsi from "inner/cjs";
5243
import * as mjsi from "inner/mjs";
53-
~~~~~~~~~~~
54-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
5544
import * as typei from "inner";
5645
cjsi;
5746
mjsi;
5847
typei;
59-
==== node_modules/inner/index.d.ts (2 errors) ====
48+
==== node_modules/inner/index.d.ts (1 errors) ====
6049
// cjs format file
6150
import * as cjs from "inner/cjs";
6251
~~~
6352
!!! error TS2303: Circular definition of import alias 'cjs'.
6453
import * as mjs from "inner/mjs";
65-
~~~~~~~~~~~
66-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
6754
import * as type from "inner";
6855
export { cjs };
6956
export { mjs };
@@ -76,12 +63,10 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
7663
export { cjs };
7764
export { mjs };
7865
export { type };
79-
==== node_modules/inner/index.d.cts (1 errors) ====
66+
==== node_modules/inner/index.d.cts (0 errors) ====
8067
// cjs format file
8168
import * as cjs from "inner/cjs";
8269
import * as mjs from "inner/mjs";
83-
~~~~~~~~~~~
84-
!!! error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("inner/mjs")' call instead.
8570
import * as type from "inner";
8671
export { cjs };
8772
export { mjs };

0 commit comments

Comments
 (0)