I tried using ES Modules, I really did...
I am an early adopter of technology, as long I don't have to pay and what better way to do that than using the latest and the greatest of the frameworks or tools while developing. This is my experience of trying to use ES Modules in my TypeScript project and my thoughts on it.
I am working on a library to scaffold discord bots, (I maintain a couple of them and a scaffold would decrease a lot of the redundant work) and I use TypeScript for that. Now, I started my project using tsdx but I soon found out that it was not maintained nowadays. So, instead of using a fork, I decided to roll out my own Typescript starter to use it myself and for the community. Enter ts-boot which is still a WIP (contributions welcome).
Since I had a clean slate in front of me, I was thinking of using the latest standards and what could be more latest than using ES Modules instead of CommonJS. TypeScript with ES2020 targeting Node 14+ and ES Modules would be bleeding edge and a dream to develop right?
Wrong!
Now before the internet people starts judging me I should state that this is not my first rodeo in TypeScript. I have a love-hate relationship with TypeScript. I keep coming back to Typescript only to get frustrated again. I love typed languages yet the disjoint way that most libraries have their code and typings separated (via DefinitelyTyped) makes things more cumbersome that it needs to be. Quite a few times I have faced the issue wherein the @types
package has not been updated for a package that has been. This causes friction that I could have avoided if I just stuck with JavaScript. But those sweet sweet types brought me back again and again.
Now don't get me wrong. I hardly face any issues nowadays. Many large packages provide their own types which makes things much smoother and for those that still have broken types, I can use any
casting to force things to work. Now you might ask, why I don't contribute by fixing the types. I do. But working with the DefinitelyTyped monorepo with its bazillion files is not exactly a great experience. I can do a bare clone in git but still, its not exactly a great experience.
So, knowing all these pains, I still decided to not only use TypeScript but also use ES Modules in my latest project. This is what my package.json
and tsconfig.json
looks like:
My problems started immediately. I was importing from fs-extra
as such:
This immediately started failing. The reason is that I need to write my imports exactly the way I want it to be in the compiled JavaScript files. So, the correct way to do it would be:
Moreover, during importing another module, one needs to use the .js
extension. For eg.
This makes my skin crawl. JavaScript is already a meme and this doesn't help the JS/TS ecosystem in any way. Moreover, .eslintrc.js
files no longer work cause ESLint still uses CommonJS to load and since we are using "type": "module"
in our package.json
, the whole project is now ES Module'd. And although ES Module projects can call CommonJS modules, the other way round is not possible. So, you either gotta rename the ESLint config to .eslintrc.cjs
or change the format to JSON. Similar actions may need to be taken for other libraries that uses a JS config file.
It was OK, till now but the final nail hit in the coffin when I found out that Jest would not work. No matter I used a JSON or a CJS file, my tests wouldn't run at all and would give a module error. I was planning to use ts-jest and my configuration looked like:
Now I don't know if I missed something or was messing something up on my part but I had already spent around 2 days working on scaffolding the library which would scaffold TypesScript project so that I could scaffold my project which scaffolds a Discord project. I threw my arms in the air, changed the config to use CommonJS and started writing this blog. Maybe I will have an easier time in a year or so.
Look forward to more posts from me regarding my experiences in working with my projects.
This blog post is not meant to be a tutorial on how to (or how not to) use ES Modules with TypeScript. Its meant to document my experiences and take it as such. But my situation might resonate with someone who have gone through a similar situation.