Run node server with PM2 - node.js

How could I run node server.js -p by pm2?
Scripts of my package.json is like below,
"scripts": {
"dev": "node server.js",
"start": "node server.js -p"
},
When I execute npm start everything work truly. But I want to run this command with pm2.
To do it when I run pm2 start npm -- start, the process will add to the list of the pm2 but my app not run!

the correct command is
pm2 start server.js
or if you want to pass -p to your app and a name
pm2 start server.js --name "my-server" -- -p

Related

kill process on port when starting node.js app?

I use nodemon to run a node.js app during development. When I save a file in the project the app restarts.
The problem is sometime the previous process on that post does not allow the app to restart.
My start script in my package.json looks like this:
"scripts": {
"start": "nodemon app.js",
"test": .....
},
When that happens I run this in terminal:
kill -9 $(lsof -t -i:4080)
Then the app works fine again.
How do I force nodemon to not try to restart until the previous process stopped and the ports is available again?
You can create an npm script that runs the kill process:
"dev": "kill -9 $(lsof -t -i:4080) && node app.js",
"start": "nodemon --exec npm run dev"
Notice in how your nodemon script you're running another script with --exec.
Start the server like you usually would:
npm start

How run pm2 with special script for prod env?

I already have the dev app run on pm2.
I wanna add the app for production.
I have a script in the package:
"scripts": {
"pm2-start-prod": "set NODE_ENV=production&& pm2 start app.js",
...
}
I tried to run pm2 start "npm run pm2-start-prod" --name backend-prod
But in pm2 appear 2 new instance app and backend-prod.
Both don't work the app logs shows 8082 port already used
backend-prod can't run script pm2-start-prod
I know I should use ecosystem, but I don't understand how.
What I did do wrong?
I think this should be working
pm2 start "whatEverScript" --name whateverName
in your case this correct
pm2 start "npm run pm2-start-prod" --name backend-prod
I think the problem in your script it should be something like this
"scripts": {
"pm2-start-prod": "set NODE_ENV=production&& node app.js",
...
}

How to chain custom script in package.json to call prestart mongod?

Trying to streamline my package.json and local development with a custom script to run Nodemon. I'm currently building an app with a front and back end I need to call mongod before start and before my custom in two tabs however I'm running into an issue.
mongod will only run in the terminal if the terminal path is set to local from testing and I've read:
Correct way of starting mongodb and express?
npm starts to execute many prestart scripts
How to npm start at a different directory
How do I add a custom script to my package.json file that runs a javascript file?
I can use prestart as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "./node_modules/.bin/nodemon app"
}
but I'm not seeing how I should chain a prestart with a custom scripts. When I try to chain it with nodemon as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "cd && mongod && ./node_modules/.bin/nodemon app"
},
Nodemon is fired first than mongodb crashes in my package.json when I call Nodemon as:
npm run nodemon
How can I start mongod before starting nodemon in my development process through one command in the package.json?

How to start a package.json script in pm2

I want to demonise my express js graphql api server. In windows local dev, I can start my server by running this command and it works fine:
yarn dev
This start command is defined in my package.json like this:
"scripts": {
"dev": "cross-env NODE_ENV=development DEBUG=express:* nodemon --exec babel-node src/index.js"
},
When I try to start this in pm2 in my linux server, I get a success like this:
latheesan#app:~/apps/tweet/server$ pm2 start yarn -- dev
[PM2] Starting /usr/bin/yarn in fork_mode (1 instance)
[PM2] Done.
However, when I type pm2 status it says error and also the display looks really odd:
I am running this on Ubuntu 16.04.
If I don't use the pm2 and just start the app in my ubuntu server with yarn dev - it runs fine.
Any ideas?
Pm2 now support npm
$ pm2 start --interpreter babel-node server.js
(or)
$ pm2 start npm --start
(or)
$ pm2 start npm --name "myAPP" --start
(or)
$ pm2 start npm --name "{app_name}" --run "{script_name}"
I have now resolved this issue.
Install the babel-node globally via: npm install -g babel-cli
Then create the pm2 config in json: pm2.json
{
"apps": [
{
"name": "Tweet GraphQL Server",
"exec_interpreter": "babel-node",
"script": "index.js",
"merge_logs": true,
"cwd": "./src",
"env": {
"NODE_ENV": "production"
}
}
]
}
Now I can run this command to start the pm2 process: pm2 start pm2.json
i do this in my app like this:
in package.json:
"scripts": {
"start": "... start application script ...",
"start:dev": "... start application script as development mode ...",
"pm2": "pm2 start npm --name \"CustomeNameForPM2\" -- run start --watch",
"pm2:dev": "pm2 start npm --name \"CustomeNameForPM2\" -- run start:dev --watch"
}
now you can run pm2 easy with npm run pm2 or npm run pm2:dev
but if you like to do something better, you can read pm2 documentation and use pm2 ecosystem config file

Why pm2 NodeJS app starts but not accessible on EC2

I have a Nodejs app running on AWS EC2. My package.json has the following instructions:
"scripts": {
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist/index.js",
"prestart": "npm run build",
..
So when connected to EC2 I (after install and cd to proj folder) I do PORT=8080 npm start The app starts fine - but messages in the console and is acessinble via my EC2 addres :8080. Also if I run PORT=8080 node dist/index.js
- also good.
But since I would like to use monitoring, restarting of the script by pm2 I try do following:
pm2 start dist/index.js -- PORT=8080
or
PORT=8080 pm2 start dist/index.js
I see that pm2 has the app started,
but it's not acessible on AWS address :8080
What do I do wrong?

Resources