I have a script in package.json like this. To run with npm I would just do it with npm start.
"scripts": {
"start": "cd build && node main"
}
I am currently trying to setup a pm2 config file for this. I created a ecosystem.json file. Neither of both of the following work with pm2 ecosystem command. What am I doing it wrong?
Note that it work if i manually type cd build && pm2 start main.js in command but this is not something i want.
First configuration:
{
"apps": [{
"name": "my-app",
"cwd": "build",
"script": "main.js"
}]
}
Second configuration
{
"apps": [
{
"name": "my-app",
"script": "npm",
"args" : "start"
}
]
}
In your code, you are giving the path incorrectly.
Use following instructions:
Hit pm2 ecosystem command, this will create a new file by name ecosystem.config.js
Remove all the code from the file and add the following code.
module.exports = {
apps : [
{
name : 'API',
script : 'build/main.js',
}
]
};
Hit pm2 start ecosystem.config.js
Check the logs using pm2 logs, your app will be started.
Hope this helps you.
I'm trying to run node app with pm2 via command line and pass only one argument with it
pm2 start app.js --node-args="41"
pm2 start app.js --env dev --node-args="41"
I tried to run it with developement environment and without it but in both ways it didn't worked.
What is the way to do it?
The pm2 command line option --node-args is for passing arguments to the Node.js V8 engine, not to the program running on that engine.
The correct way to pass arguments to your program is this:
pm2 start app.js -- 41
For dev environment:
pm2 start app.js --env dev -- 41
So basically, anything you add after the last two dashes is accessible through process.argv as an array.
You can define a process file at your project root and pass your env and args like this:
process.json
{
"apps" : [
{
"name" : "app-prod",
"script" : "app.js",
"env": {
"NODE_ENV": "production"
}
},
{
"name" : "app-dev",
"script" : "app.js",
"args" : "41",
"env": {
"NODE_ENV": "development"
}
}
]
}
This definition will allow you to pass command line argument (41) that will only be available in your development version.
Then, if you want to run development version of your app, execute it as follows
pm2 start process.json --only app-dev
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
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
I'm using the nodejs cookbook to install node and PM2 module.
include_recipe "nodejs"
nodejs_npm "pm2"
So far, so good. But when I try to start my node app via PM2 with a process.json file it starts but isn't listed in the PM2 output when I ssh into the machine and run pm2 list ... very irritating.
Am I doing something wrong is it correct to not show up there?
execute "start node app via PM2" do
cwd "/share/app"
command "pm2 startOrRestart process.json"
end
and the process.json looks like this:
{
"apps": [
{
"name": "app",
"script": "src/index.js",
"watch": true,
"ignore_watch": ["[\\/\\\\]\\./", "node_modules"],
"merge_logs": true,
"exec_interpreter": "node",
"env": {
"NODE_ENV": "local",
"port": 8080
}
}
]
}
It's because you started the process.json with another user, then when you ssh and do a pm2 list, it only list your current process users.
To avoid that you have two choices:
Do the pm2 start process.json with the same user
Set the PM2_HOME environment variable so all users will hit the same PM2 instance. Eg PM2_HOME='/etc/.pm2' pm2 start process.json && PM2_HOME='/etc/.pm2' pm2 list