I’m trying to figure out if pm2 can execute npm scripts directly instead of pointing to a specific JavaScript file. During development I use npm run dev to start my application, but when deploying to production I want to use pm2 for process management.
For development:
npm run dev
What I want to achieve in production:
pm2 start 'npm run dev'
I know that Forever process manager has a similar feature where you can specify a custom command:
forever start -c "npm run dev" ./
Is there a similar approach available in pm2, or do I need to stick with the traditional method of running pm2 start server.js directly?
pm2 handles npm scripts just fine, but watch your memory usage. When you run pm2 start npm -- run dev, pm2 spawns npm as a child process, which then starts your app. That’s an extra process layer eating more memory than running the JS file directly. I’ve seen this really add up when scaling multiple instances. For single instances, the performance hit’s basically nothing. But if you’re using cluster mode or running multiple apps on the same server, just point directly to your entry file with pm2 start app.js. The npm script route works great for dev-like production setups though.
Yes, you can execute npm scripts using pm2. The command pm2 start npm -- start or pm2 start npm -- run dev is effective for starting your development script. I’ve successfully implemented this in a production environment for a couple of years without issues. The --name parameter is optional for custom naming. A more robust method is to create an ecosystem file named ecosystem.config.js to define your scripts, allowing for better control over environment variables, instance counts, and leveraging additional pm2 features while still running your npm scripts.
yep, pm2 does this gr8! just use pm2 start npm --name "my-app" -- run dev to make it work. I’ve had it in production for ages, no issues. oh, and don’t miss those double dashes before ‘run’, that’s key!
Honestly, go with ecosystem.config.js - it’s your best option. Set it up with script: 'npm' and args: 'run dev'. Much cleaner than command line flags and you can handle env vars properly. Just don’t mix it with nodemon or you’ll get weird restart loops.
Been there with manual pm2 setups. The real problem isn’t running npm scripts - it’s all the deployment mess that follows.
Yeah, you can do pm2 start npm -- run dev or make ecosystem configs, but then you’re stuck with server configs, restarts, log monitoring, and DevOps headaches.
I ditched that for automated deployments. Push code, everything deploys automatically - builds, pm2 setup, environments, health checks, done. No more SSH or manual process management.
The automation starts your app with the right npm script, configures pm2, sets up monitoring, even handles rollbacks when things break. Like a DevOps engineer that doesn’t sleep.
You’ll fight less with servers and build more features. Check it out: https://latenode.com
Try using a shell script instead. I’ve hit stability issues running npm scripts directly through pm2, especially with complex builds using webpack dev server. Works way better if you create a simple bash script that runs the npm command, then point pm2 to that script: pm2 start ./start-dev.sh --name my-app. You get better control over environment setup and error handling, plus it avoids the argument parsing issues that sometimes happen when pm2 tries to interpret npm arguments. The ecosystem config approach is solid too, but I’ve found shell scripts more reliable across different deployments.