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

Target the Node.js module system, rather than a pure CommonJS or EcmaScript Modules host #3041

Closed
wants to merge 1 commit into from

Conversation

giorgiosironi
Copy link
Collaborator

@giorgiosironi giorgiosironi commented Feb 20, 2024

A common misconception is that node16 and nodenext only emit ES modules. In reality, node16 and nodenext describe versions of Node.js that support ES modules, not just projects that use ES modules. Both ESM and CommonJS emit are supported, based on the detected module format of each file. Because node16 and nodenext are the only module options that reflect the complexities of Node.js’s dual module system, they are the only correct module options for all apps and libraries that are intended to run in Node.js v12 or later, whether they use ES modules or not.

Also shown in the tsconfig suggested configuration.

To use EcmaScript Modules coming from any library, we will likely need to start emitting EcmaScript Modules, as

require cannot reference an ES module. For TypeScript, this includes import statements in files that are detected to be CommonJS modules, since those import statements will be transformed to require calls in the emitted JavaScrip

This change should still emit CommonJS modules. To start emitting ES modules, try:

adding "type": "module" to the project package.json

@giorgiosironi
Copy link
Collaborator Author

My understanding is this will fail until axios and axios-cache-interceptor will release a combination of .cts files that allows a chain of dependencies entirely built of CommonJS modules rather than mixing them with ESM:
arthurfiorette/axios-cache-interceptor#778 which in turn depends on axios/axios#6218

@erkannt
Copy link
Member

erkannt commented Mar 13, 2024

Have confirmed locally that axios/axios#6218 would fix this issue for us.

cd sciety
npm remove axios
cd ..
gh repo clone remcohaszing/axios
git checkout esm-export-cjs-types
npm ci
cd ../sciety
npm install ../axios
npx tsc --noEmit

@giorgiosironi
Copy link
Collaborator Author

This is blocked on the external packages, closing and letting it re-emerge when we look at ESM through reserved capacity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants