AlexSNorth
AlexSNorth

Reputation: 153

How to uglify/minify NodeJS application

For work I've been developing in Node.js to create our web-based application, this application is gradually becoming larger and larger and I was wondering if there was a way to uglify/minify the server-side files and code? I've been looking all over the internet and I can only find examples to send minified/uglified/compressed files to the client's web browser to lower the load. And I really want to get any performance boost I can get server side as well.

Now my question is two-fold: 1) Does minifying/uglifying/compressing the server-side code actually positively impact the performance of the Node.js server? 2) If so, what's a good way to get this to happen aside from manually doing it to every file in the entire project? (There are A LOT!)

Upvotes: 1

Views: 3694

Answers (2)

AlexSNorth
AlexSNorth

Reputation: 153

Just going to answer myself here so this question can be closed.

@CertainPerformances and @jfriend00 both pointed this out:

*Minifying the server code may have a tiny difference in startup time due to the smaller file I/O time.

But there most likely isn't a performance boost after the server is up and running. Add to that that it complicates the build, the development process, and debugging.*

Upvotes: 2

VPaul
VPaul

Reputation: 1013

It's unlikely that you would see anyone publishing un-minified code on their production server. I've tried that myself and the difference in size is substantial not just in size but also in loading times especially if there are multiple un-minified files.

Just imagine that you have so many dependencies in your app (react/vue, various npm packages and your custom code) and none are compressed/minified. You are looking at your JS code at mega-bytes. This is not desirable. If the load time increases for any site by even 3 seconds, it gets negatively viewed by the users and there's a high rejection rate. I would say minifying is a must and there should be a single JS file. As more the number of resources fetched from the server, the slower it gets.

The good thing about NPM is that its so simple to get your code minified. You can use webpack (https://www.npmjs.com/package/webpack) for the same. You can add a build script in your package.json file that takes care of all this and you don't even notice. Then all your code gets placed in one folder. You can choose what files you want in that folder from package.json configs (https://docs.npmjs.com/files/package.json#files). Then when you upload your app to the server, you just move this folder over there and that's it.

The next time you make any changes to the code, you again run this build script and this build folder is updated, which can again be uploaded to update your code on production. This is a very naive way of doing deployments, but is a bare minimum for any web project.

Upvotes: 0

Related Questions