Is bundle necessary in a pure-backend NodeJS app?

Viewed 1827

I'm quite new in this overwhelmingly huge world of js-stack-tools and recently I've been following some tutorials about bundlers, which seem to include almost always the frontend facet.

But in a simple app with only server-side participation (like a single service app in a microservices architecture) is it necessary to bundle the app? what are the pros/cons of doing it?

If pros > cons would be a good practice to use Jest's snapshots to check the bundle.js file?

My apologizes if the question turns to be a bit ambiguous but I'm struggling trying to fit all these new concepts in my head.

2 Answers

Hope you got an idea in the comments above. Just to add some more points here -

  1. Using babel - If you want to use upcoming js features in your sever-side code, you might want to use Babel to compile your code down to the set of features supported by the current version of your Node. Having a build step for this might help your case.

  2. Removing comments and other optimizations - Using a build step would also help to clean up your server code, and would also allow for some optimizations.

  3. Debugging- From my experience, debugging built code is pain and source-maps don't always work fine. Use at your own risk.

  4. Always rely on logging / APM - Have some way to log your important points in your program or better yet have an APM do that for you.

Hope this helps.

I would recommend that you bundle code, because of tree shaking.

The Node.js import/require statement will import the entire module (if the library author exports all things in one file), including any features you don't use. This holds true whether the module is an CommonJS module or ESM.

The downside of importing entire modules is their memory usage. Smaller, less dependent modules take up less memory; larger, more dependent modules take up more memory. In one case I encountered, a pure (no side effects) module import took up 20MB of memory, which would have been reduced to KB if the code had been bundled.

Related