How to start a yarn script with pm2 on Windows? - node.js

I'm in a project using yarn and want to start one of the scripts in the package.json with pm2 using this command:
pm2 start "yarn start"
It results in:
[PM2] Applying action restartProcessId on app [yarn](ids: [ 0 ])
[PM2][ERROR] Process 0 not found
I also tried:
pm2 start yarn -- start
Which gets me:
[PM2][ERROR] Script not found: C:\path\to\project\yarn start
The package.json is located at C:\path\to\project\package.json and has a script called "start".

yarn run pm2 start server.js
You must be in your target directory
This is our solution to running pm2 with snap
still working on startup script for yarn pm2

Related

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",
...
}

pm2 command not working from a docker image

I have a pm2 command in my docker file which should run inside container when the container starts:
CMD ["pm2", "start", "ecosystem.local.json"]
CMD ["pm2", "logs"]
Expected output: showing logs of my process
Actual output: no logs but container is running.
Seems like the first command doesnt start the process because when i go inside the container and run pm2 list manually then i dont see any process running.
But when i run the same above commands from inside the container manually:
pm2 start ecosystem.local.json
pm2 list
pm2 logs
then i can see my process running and its logs.
So why the command CMD ["pm2", "start", "ecosystem.local.json"] doesnt run automatically from the docker file/image?
P.S. I also used pm2-runtime from https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/ but no success.
As pointed out in comment, my container was getting started and immediately exiting after that. I was using pm2 and switching to pm2-runtime did the work.
Not working:
"start-server": "export NODE_ENV=production && cd server && ./node_modules/.bin/pm2 start index.js",
Working:
"start-server": "export NODE_ENV=production && cd server && ./node_modules/.bin/pm2-runtime start index.js",
From Dockerfile-
CMD ["yarn", "start-server"]

How to pass NODE_ENV=production to pm2?

I have this node app deployed and runs fine with NODE_ENV=production yarn start.
I can demonize the app using
pm2 start npm -- start
but then it defaults to NODE_ENV=development config.
And when I use
pm2 start npm -- start NODE_ENV=production
It still starts with development config.
Also I tried passing the env using a process.yml file
apps:
- script : index.js
watch: true
instances: 4
env :
NODE_ENV: production
but pm2 start npm -- start process.yml still loads the development configs.
How can I fix this?
The reason you might be facing this is because you would have started pm2 with development once. Now it will use that env until you kill it. Following these steps should help
./node_modules/.bin/pm2 kill
NODE_ENV=production ./node_modules/.bin/pm2 start server.js
You can also use --update-env. From the official docs
By default we want that PM2 doesn’t change process environment while
restarting or reloading so they are immutable. If you want to update
them, you must use --update-env :
try this:
export NODE_ENV=production&&pm2 start server.js
export NODE_ENV=production && pm2 start ecosystem.config.js
Following line helped me! Thanks to #mehdi parastar and #abhinavd
Try this, if you are using a pm2, the below code works for me
sudo NODE_ENV=production pm2 start app.js
You can solve it using this command (with double quotes):
sudo pm2 start "NODE_ENV=production yarn start"
You just do it like this ->
NODE_ENV=production pm2 restart <id|name> --update-env
You can check the docs here:
PM2 - Update Env Variables docs

How to start pm2 process with DEBUG option

I have an express app that I start in terminal with following command to enable debug logs in it:
DEBUG=custom:* npm start (on Ubuntu)
SET DEBUG=custom:* & npm start (on Windows)
On production server, I start app with PM2 using following command:
pm2 start bin/www -i 0
But this does not enable the debug logs in my code, so the debug statements are not added to the logs, only console.error() are added to the log files. How can I pass the DEBUG=custom:* option while starting my app with PM2?
Try DEBUG='custom:*' pm2 start bin/www -i 0
If you are restarting an existing process add the --update-env flag:
DEBUG='custom:*' pm2 restart bin/www -i 0 --update-env
Mikko was correct but if you add that to package.json scripts, it won't work!
"scripts": {
"start": "DEBUG='custom:*' pm2 start bin/www -i 0",
...
},
Because DEBUG='custom:*' is given to pm2 process NOT your process. So in this case you have to use an ecosystem file and add DEBUG setting in ecosystem file, e.g.
"scripts": {
"start": "pm2 start ecosystem.config.js",
...
},
//in ecosystem.config.js add this
env: {
NODE_ENV: 'development',
DEBUG: 'custom:*'
},

Can pm2 run an 'npm start' script

Is there a way for pm2 to run an npm start script or do you just have to run pm2 start app.js
So in development
npm start
Then in production with pm2 you would run something like
pm2 start 'npm start'
There is an equivalent way to do this in forever:
forever start -c "npm start" ./
PM2 now supports npm start:
pm2 start npm -- start
To assign a name to the PM2 process, use the --name option:
pm2 start npm --name "app name" -- start
Those who are using a configuration script like a .json file to run the pm2 process can use npm start or any other script like this -
my-app-pm2.json
{
"apps": [
{
"name": "my-app",
"script": "npm",
"args" : "start"
}
]
}
Then simply -
pm2 start my-app-pm2.json
Edit - To handle the use case when you have this configuration script in a parent directory and want to launch an app in the sub-directory then use the cwd attribute.
Assuming our app is in the sub-directory nested-app relative to this configuration file then -
{
"apps": [
{
"name": "my-nested-app",
"cwd": "./nested-app",
"script": "npm",
"args": "start"
}
]
}
More detail here.
To use npm run
pm2 start npm --name "{app_name}" -- run {script_name}
I needed to run a specific npm script on my app in pm2 (for each env)
In my case, it was when I created a staging/test service
The command that worked for me (the args must be forwarded that way):
pm2 start npm --name "my-app-name" -- run "npm:script"
examples:
pm2 start npm --name "myApp" -- run "start:test"
pm2 start npm --name "myApp" -- run "start:staging"
pm2 start npm --name "myApp" -- run "start:production"
Hope it helped
Yes. Use pm2 start npm --no-automation --name {app name} -- run {script name}. It works. The --no-automation flag is there because without it PM2 will not restart your app when it crashes.
you need to provide app name here like myapp
pm2 start npm --name {appName} -- run {script name}
you can check it by
pm2 list
you can also add time
pm2 restart "id" --log-date-format 'DD-MM HH:mm:ss.SSS'
or
pm2 restart "id" --time
you can check logs by
pm2 log "id"
or
pm2 log "appName"
to get logs for all app
pm2 logs
I wrote shell script below (named start.sh).
Because my package.json has prestart option.
So I want to run npm start.
#!/bin/bash
cd /path/to/project
npm start
Then, start start.sh by pm2.
pm2 start start.sh --name appNameYouLike
Yes we can, now pm2 support npm start, --name to species app name.
pm2 start npm --name "app" -- start
See to enable clustering:
pm2 start npm --name "AppName" -i 0 -- run start
What do you think?
If you use PM2 via node modules instead of globally, you'll need to set interpreter: 'none' in order for the above solutions to work. Related docs here.
In ecosystem.config.js:
apps: [
{
name: 'myApp',
script: 'yarn',
args: 'start',
interpreter: 'none',
},
],
pm2 start npm --name "custom_pm2_name" -- run prod
"scripts": {
"prod": "nodemon --exec babel-node ./src/index.js"
}
This worked for me when the others didnt
You can change directory to your project
cd /my-project
then run
pm2 start "npm run start" \\ run npm script from your package.json
read more here
For the normal user
PM2 now supports npm start:
pm2 start npm -- start
To assign a name to the PM2 process, use the "--name" option:
pm2 start npm --name "your desired app name" -- start
For the root user
sudo pm2 start npm -- start
To assign a name to the PM2 process, use the "--name" option:
sudo pm2 start npm --name "your desired app name" -- start
Yes, Absolutely you can do it very efficiently by using a pm2 config (json) file with elegance.
package.json file (containing below example scripts)
"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.
ecosystem.config.json file
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
It's working fine on CentOS 7
PM2 version 4.2.1
let's take two scenarios:
1. npm start //server.js
pm2 start "npm -- start" --name myMainFile
2. npm run main //main.js
pm2 start "npm -- run main" --name myMainFile
Unfortunately, it seems that pm2 doesn't support the exact functionality you requested https://github.com/Unitech/PM2/issues/1317.
The alternative proposed is to use a ecosystem.json file Getting started with deployment which could include setups for production and dev environments. However, this is still using npm start to bootstrap your app.
pm2 start ./bin/www
can running
if you wanna multiple server deploy
you can do that. instead of pm2 start npm -- start
Don't forget the space before start
pm2 start npm --[space]start
so the correct command is:
pm2 start npm -- start
To run PM2 with npm start method and to give it a name, run this,
pm2 start npm --name "your_app_name" -- start
To run it by passing date-format for logs,
pm2 start npm --name "your_name" --log-date-format 'DD-MM HH:mm:ss.SSS' -- start
Now, You can use after:
pm2 start npm -- start
Follow by https://github.com/Unitech/pm2/issues/1317#issuecomment-220955319
for this first, you need to create a file run.js and paste the below code on that.
const { spawn } = require('child_process');
//here npm.cmd for windows.for others only use npm
const workerProcess = spawn('npm.cmd', ['start']);
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('child process exited with code ' + code);
});
and run this file with pm2.
pm2 start run.js
List item

Resources