I was thinking into starting a Node.js project from scratch and had an unsettling choice to make: to use ESM or CommonJS.
The decision of a few po...
For further actions, you may consider blocking this person and/or reporting abuse
Thank you so much for this. I seriously had this problem today when dealing with imports from typescript to js and mjs.
Thanks Ale :) Was struggling to understand current state of Node + TS + ESM support and you nailed it here.
Glad it was helpful to you Pablito!
Nice write-up Alejandro!
Do you find it a better alternative to avoid the "type":"module" and instead just use "main" and "module" as keys (potentially with "exports" too) to avoid the "type" declaration which hardly requires the project to run as a module too?
Thanks @lirantal! I have tried alternatives but in the end
type
was simpler and easier for people to understand. I feel like "exports" is a rabbit hole where you need to know exactly what you're doing or you are lost.Yes agree that exports makes it a bit more harder on discovery :-)
Thanks!
Can someone, for the love of all that is good, please explain to me in a way that actually makes sense WHY anyone would want to import something with an extension? PLEASE! Help me understand why ANYONE would want to do this?
I tried the above .swcrc file. It didn't seem to work. I checked on playground got alot of errors.Finally i tried the below config it worked.Thanks for explaining how to do it.
{
"jsc": {
"parser": {
"syntax": "typescript",
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
.swcrc
Have you tried using ts-node/register instead of tsx?
I did, for me
tsx
seems simpler to use.Did you happen to try unjs.io/packages/jiti together with unjs.io/packages/listhen
I have not! I might give them a try but so far I didn't had major issues with the approach described here
Thank you!
I've run ts code using @swc-node/register/esm like so "NODE_ENV=dev node --loader @swc-node/register/esm src/index.ts"