DEV Community

Cover image for Rescuing legacy Node.js projects with Bun
Sadman Yasar Sayem
Sadman Yasar Sayem

Posted on

Rescuing 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 require() and module.exports, then you have already been using CommonJS. However, this is no longer the standard approach to developing modern Node.js applications and you need to use ESModules. So instead of require() and module.exports, you need to use import and export default statements. Unfortunately, some npm packages are being migrated to ESModules and this is an issue for projects built with CommonJS. ESModules are backwards compatible with CommonJS but its not the same way around. This has been a big issue in the Node.js ecosystem and it can be quite difficult to migrate large codebases to ESModules.

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 that had similar ambitions, 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 statemtents in the same file. This allows you to keep using CommonJS modules and use import statemtents 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.

Thanks for reading. :)

Top comments (2)

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Now that is interesting. Thanks for posting.

I'm interested that you were able to do this without starting to use Bun packages - I'm worried about them, though I guess you are now dependent on Bun not failing, you've limited your exposure.

Collapse
 
sadmanyasar profile image
Sadman Yasar Sayem

Totally agree with you about relying on Bun not failing for the project. However, I believe it has a great community support and the team is regularly fixing compatibility issues with npm libraries.