I have seen many questions here but that was not working for me. That why asking this question?
I am using DigitalOcean Ubuntu 16.04 server. I have a node project run by "npm start".
Script is:
"scripts": {
"start": "node ./bin/www"
}
generally pm2 work for
pm2 start app.js
As my script is like this and run by npm start
how can I run my server forever.
You can run built-in npm scripts like this:
pm2 start npm -- start
If you have a custom script, you can run like this:
pm2 start npm -- run custom
--
In your case, pm2 start npm -- start
would run node ./bin/www
. Change the start
script to node app.js
if you want to run node app.js
.
npm start && node app.js have difference. it depend on the start script of your project.
In your case the above answer is right and it will run by pm2 start npm -- start
.
but if it also not work for you ( I don,t know why it not working for you as you haven,t share any error to us) you can change start script.
To do that you have to change on your code from your bin/www
file you have to take the code what running the server
example:
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
and the others you have written on your www
file to run the server have to move app.js or main file then you can try:
pm2 start app.js
or
forever start app.js
Yes, you can do it very efficiently by using a pm2 config (json) file with elegance.
"scripts": {
"start": "concurrently npm:server npm:dev",
"dev": "react-scripts start",
"build": "node ./scripts/build.js",
"eject": "react-scripts eject",
"lint": "eslint src server",
"shivkumarscript": "ts-node -T -P server/tsconfig.json server/index.ts"
}
Suppose we want to run the script named as 'shivkumarscript' with pm2 utility. So, our pm2 config file should be like below, containing 'script' key with value as 'npm' and 'args' key with value as 'run '. Script name is 'shivkumarscript' in our case.
module.exports = {
apps: [
{
name: "NodeServer",
script: "npm",
automation: false,
args: "run shivkumarscript",
env: {
NODE_ENV: "development"
},
env_production: {
NODE_ENV: "production"
}
}
]
}
Assuming that you have already installed Node.js, NPM and PM2 on your machine. Then below should be the command to start the application through pm2 which will in turn run the npm script (command line mentioned in your application's package.json file):
For production environment:
pm2 start ecosystem.config.js --env production --only NodeServer
For development environment:
pm2 start ecosystem.config.js --only NodeServer
...And Boooom! guys
My application is called 'app.js'. I have the exact same start script for an express Node.js application.
And after googling tons this solved my problem, instead of calling pm2 start app.js.
pm2 start ./bin/www
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.