Sadman Yasar Sayem
Saving legacy Node.js projects with Bun
For the past 4 months, I have been working in a legacy MERN stack project that is a booking system for a sports complex in Malaysia where I developed the website, backend API and an admin panel using AdminJS. It was all functional and allowed rapid iterations for requirements until a new one was added: export data as CSV.
The API and the admin panel are all using CommonJS modules. If you have ever written code using
1
require()
1
module.exports
1
require()
1
module.exports
1
import
1
export
The admin panel built with AdminJS was in version 6.x which still has support for CommonJS. As I tried to implement the import/export plugin, there was this issue I couldnt resolve at all. I knew this was something to do with the AdminJS version so I upgraded to version 7.x, only to realize that this version dropped support for CommonJS completely. So the only option I had was to use the unmaintained cjs-to-es6 package to rewrite all CommonJS code with ESM. Then I saw the bun's announcement: It got released on Windows!.
When I saw the release of bun six months ago, I was not that hyped as I saw a tool similar to this that tried to do pretty much the the same, Rome, and dissapointed many. But it was different this time. It really is a drop in replacement for Node.js so you can start using it by replacing the npm and node commands in your package.json file. The main feature that captured my interest was the ability to use require and import statements in the same file. This allows you to keep using CommonJS modules and use import statements for any new modules that drop support for it. The only catch I could find so far is that if you decide to mix import and require statements, you cannot use module.exports but instead use export statement. I did exactly that and now I have a fully functional backend with admin panel that won't make your head scratch fighting with CommonJS and ESModules.