79640728

Date: 2025-05-27 15:05:49
Score: 0.5
Natty:
Report link

There CAN BE a need to build your nodeJS application.

If you are using exec_mode: 'fork', you don't have to build, but if you want to go exec_mode: 'cluster' then yes you need to build.

if you don't build in cluster mode and try to serve directly from your index.js (or whatever is your initial file), only first instance of the clustesters will work, and the rest of the instances will fail by "Error: listen EADDRINUSE: address already in use :::8080", and it will be subtle because without building pm2 logs won't work for clustered application. (do not try to see with pm2 logs) you see the error with:

tail -f ~/.pm2/pm2.log

assuming you want to use cluster mode. you can create build in package.json:

"build": "rm -rf dist  && babel . --out-dir  --ignore \"node_modules,dist\" --copy-files --extensions \".js\""

then in your ecosystem.config.js:

cwd: './YourProject/',
script: 'dist/index.js',
exec_mode: 'cluster',
instances: 'max' // or count of the instances

My current issue is when i build and serve from the build, the ram usage is doubled, when served directly it is fine when server is idle. This could be related to my build script, so be careful. And if you find that problem with "higher memory when serving from the build" issue, i would love to hear about it too, cheers!

Reasons:
  • Blacklisted phrase (1): cheers
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Oytun