Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support require(esm) in --module nodenext #60761

Merged
merged 11 commits into from
Jan 23, 2025
Prev Previous commit
Next Next commit
Do not error on require(esm) in node20/nodenext
  • Loading branch information
andrewbranch committed Dec 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a3a9f405c49455fcbfe278cba8019814a2728dbc
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -4714,7 +4714,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (errorNode && resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
errorOnImplicitAnyModule(/*isError*/ false, errorNode, currentSourceFile, mode, resolvedModule, moduleReference);
}
if (errorNode && (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext)) {
if (errorNode && (moduleKind === ModuleKind.Node16 || moduleKind === ModuleKind.Node18)) {
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l));
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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.
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.


!!! 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
// esm format file
import * as self from "package";
self;
==== index.cjs (1 errors) ====
==== index.cjs (0 errors) ====
// esm format file
import * as self from "package";
~~~~~~~~~
!!! 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.
self;
==== package.json (0 errors) ====
{
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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.
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.


!!! 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
// esm format file
import * as self from "package";
self;
==== index.cjs (1 errors) ====
==== index.cjs (0 errors) ====
// esm format file
import * as self from "package";
~~~~~~~~~
!!! 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.
self;
==== package.json (0 errors) ====
{
84 changes: 3 additions & 81 deletions tests/baselines/reference/nodeModules1(module=node20).errors.txt

Large diffs are not rendered by default.

84 changes: 3 additions & 81 deletions tests/baselines/reference/nodeModules1(module=nodenext).errors.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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.
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.
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.
node_modules/inner/index.d.mts(2,13): error TS2303: Circular definition of import alias 'cjs'.
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.

@@ -38,15 +36,11 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
mjsi.mjsSource;
typei.mjsSource;
ts.mjsSource;
==== index.cjs (2 errors) ====
==== index.cjs (0 errors) ====
// cjs format file
import * as cjs from "package/cjs";
import * as mjs from "package/mjs";
~~~~~~~~~~~~~
!!! 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.
import * as type from "package";
~~~~~~~~~
!!! 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.
cjs;
mjs;
type;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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.
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.
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.
node_modules/inner/index.d.mts(2,13): error TS2303: Circular definition of import alias 'cjs'.
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.

@@ -38,15 +36,11 @@ node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import
mjsi.mjsSource;
typei.mjsSource;
ts.mjsSource;
==== index.cjs (2 errors) ====
==== index.cjs (0 errors) ====
// cjs format file
import * as cjs from "package/cjs";
import * as mjs from "package/mjs";
~~~~~~~~~~~~~
!!! 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.
import * as type from "package";
~~~~~~~~~
!!! 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.
cjs;
mjs;
type;
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
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.
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.
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.
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.
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.
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.
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.


!!! 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
cjsi;
mjsi;
typei;
==== index.cjs (3 errors) ====
==== index.cjs (0 errors) ====
// cjs format file
import * as cjs from "package/cjs";
import * as mjs from "package/mjs";
~~~~~~~~~~~~~
!!! 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.
import * as type from "package";
~~~~~~~~~
!!! 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.
cjs;
mjs;
type;
import * as cjsi from "inner/cjs";
import * as mjsi from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as typei from "inner";
cjsi;
mjsi;
typei;
==== node_modules/inner/index.d.ts (2 errors) ====
==== node_modules/inner/index.d.ts (1 errors) ====
// cjs format file
import * as cjs from "inner/cjs";
~~~
!!! error TS2303: Circular definition of import alias 'cjs'.
import * as mjs from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as type from "inner";
export { cjs };
export { mjs };
@@ -76,12 +63,10 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
export { cjs };
export { mjs };
export { type };
==== node_modules/inner/index.d.cts (1 errors) ====
==== node_modules/inner/index.d.cts (0 errors) ====
// cjs format file
import * as cjs from "inner/cjs";
import * as mjs from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as type from "inner";
export { cjs };
export { mjs };
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
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.
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.
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.
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.
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.
node_modules/inner/index.d.ts(2,13): error TS2303: Circular definition of import alias 'cjs'.
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.


!!! 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
cjsi;
mjsi;
typei;
==== index.cjs (3 errors) ====
==== index.cjs (0 errors) ====
// cjs format file
import * as cjs from "package/cjs";
import * as mjs from "package/mjs";
~~~~~~~~~~~~~
!!! 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.
import * as type from "package";
~~~~~~~~~
!!! 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.
cjs;
mjs;
type;
import * as cjsi from "inner/cjs";
import * as mjsi from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as typei from "inner";
cjsi;
mjsi;
typei;
==== node_modules/inner/index.d.ts (2 errors) ====
==== node_modules/inner/index.d.ts (1 errors) ====
// cjs format file
import * as cjs from "inner/cjs";
~~~
!!! error TS2303: Circular definition of import alias 'cjs'.
import * as mjs from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as type from "inner";
export { cjs };
export { mjs };
@@ -76,12 +63,10 @@ node_modules/inner/index.d.ts(3,22): error TS1479: The current file is a CommonJ
export { cjs };
export { mjs };
export { type };
==== node_modules/inner/index.d.cts (1 errors) ====
==== node_modules/inner/index.d.cts (0 errors) ====
// cjs format file
import * as cjs from "inner/cjs";
import * as mjs from "inner/mjs";
~~~~~~~~~~~
!!! 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.
import * as type from "inner";
export { cjs };
export { mjs };
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
error TS2210: The project root is ambiguous, but is required to resolve import map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
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("#mjs")' call instead.
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("#type")' call instead.


!!! error TS2210: The project root is ambiguous, but is required to resolve import map entry '.' in file 'package.json'. Supply the `rootDir` compiler option to disambiguate.
@@ -20,15 +18,11 @@ index.cjs(4,23): error TS1479: The current file is a CommonJS module whose impor
cjs;
mjs;
type;
==== index.cjs (2 errors) ====
==== index.cjs (0 errors) ====
// esm format file
import * as cjs from "#cjs";
import * as mjs from "#mjs";
~~~~~~
!!! 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("#mjs")' call instead.
import * as type from "#type";
~~~~~~~
!!! 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("#type")' call instead.
cjs;
mjs;
type;
Loading