CJS/ESM issue with arktype
I installed arktype. It's a ESM thing, and it broke my building process.
To fix it, I had to switch to building with babel and had to set these values in tsconfig.json::
However, now ts-node stopped working and hence - mocha and all my tests written in TS:
Any ideas?
49 Replies
Here is a sandbox where you can see the origin of this problem
OnkelTem
replit
NodeJS TypeScript 1
Run Node.js code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
Just hit the "Run" button
The code is as simple as:
I have no idea why this would happen. I guess ts-node cares about module resolution settings in tsconfig. Try using
NodeNext
or something?
It should be able to resolve the CJS exports that are specified in package.json
I bet that's what it is there's a resolvePackageJsonExports
flag that is enabled by default for NodeNext
and maybe some others maybe try enabling that?
ES2015
is hella oldOk I updated it.
https://replit.com/@OnkelTem/NodeJS-TypeScript-1
OnkelTem
replit
NodeJS TypeScript 1
Run Node.js code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
Here is a new version. I also removed not-used deps and @tsconfig/* package, bringing its content into project's tsconfig.json
So not it's this:
I don't understand this UI why is it so bad
Like I can only see the command line and there's no where obvious for me to click to see files unless I just reload
Hm. Are you logged in?
Can you repro it on StackBlitz that's where I usually test AT
I tried to sign up and just got this
Let's figure out the heck is it with sharing repl.it
I created a posting on their forum, waiting for reply
I'm not sure if I can reprodice it on stackblitz. I thought it's a client-side thing only
maybe I'm wrong
Artiom Neganov
StackBlitz
Typescript + ArkType (forked) - StackBlitz
Blank starter project for building TypeScript apps.
Not the real deal actually, but an error is displayed anyway
I've an answer on repl.it forum already!
Not sure if you figured that out yourself, but...
This button should show the source 🙂
Wow that's such bad UX haha
That looks like an embed link
Anyways I think what you need to do is change
"moduleResolution": "Node",
You only want NodeNext etc. if you're using ESMOnkelTem
replit
NodeJS TypeScript 1
Run Node.js code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
I set it to NodeNext
I know I meant you have to set it to
"moduleResolution": "Node",
To use CJSAh, I see. Well, that I tried as well
Can you change module as well to CommonJS or something?
1. Try esbuild-kit/tsx runner
2. Try adding
export {}
This seems to be working, yes.
So the only working configuration so far is:
Yeah that actually makes sense you need those settings for CJS I just haven't used it in a long time
It's interesting that
"moduleResolution": "Node16"
doesn't workNode16 is ESM
Ah, I see then
So I use a bundler (babel), but cannot set it to
Bundler
because ts-node starts to look into tsconfig and make false asumptions
Probablt it doesn't understand Bundler
?
MehI don't know that might be a good question for the TS discord
A few days ago they helped me to discover this "Bundler" options which seemed to be a good solution!
Until I tried it with ts-node LOL
Is there a reason you can't use ESM? Is it a legacy work project?
I don't know really. Don't want this plague to spread further LOL
ESM makes things easier
in what way?
ESM can import CJS the reverse isn't true
What I've been through for the last few days - I wouldn't call easy
I mean granted this is a pain especially if you're new to it
Basically if you have a choice to get it to work with either CJS or ESM, ESM will be easier and better for the long term
But that's not an advantage, right? It's the source of the problems. For this reason I cannot use ESM with commonJS. And ESM - is a system that forces me to switch. I'm not a conspiracy theory follower, but it may look like a deliberate thing, while it's just an architecture consequence
No the reason you were getting that error is because your project was specified as CJS because you don't have
"type": "module"
in your package.json
You will have to make some slight adjustments to enable this setting but again the biggest advantage is you can import CJS from ESM but not the reverse, and some projects only support ESMI don't use such projects, yeah. I use previous versions, cjs compatible
@OnkelTem try tsx runner, it can import es from cjs
Are it building a lib or app?
A lib