Skip to content

Module nodenext is resolving to cjs build incorrectly #1778

Description

@perrin4869

Search Terms

nodenext

Expected Behavior

tsc and ts-node resolve to the same modules when using nodenext.

Actual Behavior

ts-node is resolving to the commonjs build of a dependency, even though tsc resolves to the esm build.

Steps to reproduce the problem

Using the reproduction below, notice:

11:34:45 {master} ~/ts-node-nodenext-bug$ npm install
11:35:13 {master} ~/ts-node-nodenext-bug$ npx tsc
11:35:28 (master) ~/ts-node-nodenext-bug$ node --loader ts-node/esm index.ts
(node:25884) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/index.ts:843
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: ⨯ Unable to compile TypeScript:
index.ts:3:7 - error TS2322: Type 'typeof import("/home/perrin4869/ts-node-nodenext-bug/foo/cjs/index")' is not assignable to type'number'.
3 const bar: number = foo;~~~
at createTSError (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/index.ts:843:12)
at reportTSError (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/index.ts:847:19)
at getOutput (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/index.ts:1057:36)
at Object.compile (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/index.ts:1411:41)
at transformSource (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/esm.ts:400:37)
at /home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/esm.ts:278:53
at async addShortCircuitFlag (/home/perrin4869/ts-node-nodenext-bug/node_modules/ts-node/src/esm.ts:409:15)
at async ESMLoader.load (node:internal/modules/esm/loader:407:20)
at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:11)
at async link (node:internal/modules/esm/module_job:70:21) {
diagnosticCodes: [ 2322 ]
}

I think this has to do with the fact that foo is using package.json with "type": "commonjs" to specify the commonjs build.
If this is changed to a cjs file, the problem does not occur

Minimal reproduction

https://github.com/perrin4869/ts-node-nodenext-bug

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions