chef setup: trying to start node app with PM2 - node.js

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

Related

How to use pm2 start npm in a specific directory?

Is there a CLI tag for running pm2 start npm -- start in a specific directory? I looked around but could not find an answer.
On the other hand, when I run pm2 without npm, I can specify which directory I want to run pm2 in. For example:
pm2 start /opt/www/myapp/index.js
Is there any way to add a path tag to the pm2 start npm -- start command?
You can use something like this:
cd /directory/of/my/app ; pm2 start npm -- start
You can also write an ecosystem file to parameter you app:
{
"apps": [
{
"name": "my-app",
"cwd": "/path/to/app",
"script": "npm",
"args": "start"
}
]
}
To generate an empty ecosystem file:
pm2 init simple
this will generate a file named ecosystem.config.js that you can rename.
Then to start application:
pm2 start ecosystem.config.js
ecosystem doc: https://pm2.keymetrics.io/docs/usage/application-declaration/

PM2 Cluster Mode - Cannot find module 'dotenv/config'

I am trying to run multiple apps using PM2 in cluster mode with config file given below:
"apps": [
{
"name": "Node APIs",
"script": "./server",
"watch": true,
"node_args": "-r dotenv/config",
"instances": "max",
"exec_mode": "cluster"
},
{
"name": "Node Batch",
"script": "./batch_process",
"watch": true,
"node_args": "-r dotenv/config"
}
]
}
Node APIs process is getting errored in pm2 list while Node Batch Process works fine. When I check ~/.pm2/pm2.logs it says:
Cannot find module 'dotenv/config'
I have installed dotenv module both locally and globally but still showing same error.
Also PM2 cluster mode works fine in my local machine but on AWS EC2 it shows above error. What am I missing?
PM2: v4.4.0
NodeJS: v8.12.0
After ages of looking and experimenting, it seems that it doesn't work in cluster mode, but it does in fork mode. Try running it in fork mode.
Try specifying the full path to your package in node_modules via the node_args parameter, even if you are already specifying it in cwd.
It will work in cluster mode.
{
name: 'app-api',
script: '/full/path/to/app/api.js',
instances: 2,
exec_mode: 'cluster',
cwd: '/full/path/to/app',
node_args: ['-r', '/full/path/to/app/node_modules/dotenv/config'],
}

pm2 start in subdirectory

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.

how to pass custom arguments to app

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

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

Resources