I'm trying to use ESM and TypeScript in Node.js v14. This is not hard and exist some questions for it:
- How to use ts-node ESM with node modules?
- Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts
Try to summarize it in tsconfig.json
:
{
"compilerOptions": {
"module": "es2022",
"esModuleInterop": true
},
"ts-node": {
"esm": true
}
}
If not provide
esm: true
, you can usets-node-esm
instead ofts-node
For example:
npx ts-node-esm my-code.ts
But main problem is that when using nyc, it can't correctly collect the statements/branches that have passed.
I have tried nyc-config-typescript and esm-loader-hook but none of these works.
How can I handle this problems?