pm2 launching from a JSON not working - node.js

I'm trying to launch my app from an app.json file:
{
"apps": [{
"name": "app",
"cwd": "/Users/xxx/Sites/Projects/api/",
"script": "bin/www",
"instances": 1,
"exec_mode": "fork",
"env": {
"NODE_ENV": "dev",
"PORT": "3000"
},
"env_production" : {
"NODE_ENV": "production",
"PORT": "3000"
},
"log_file": "access.log",
"error_file": "error.log",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"watch": "true",
"ignore_watch": [""]
}]
}
The problem comes when I use the JSON to launch my app.
pm2 start applist.json
[PM2][WARN] Applications app not running, starting...
If I launch the app without a JSON, it works like a charm.
pm2 start bin/www --watch
Any help with this?

Related

How to merge env variables for PM2 commands along with existing env variables?

I have a pm2 configuration json file like below, is there any way to merge the env variable while executing the pm2 commands.
like pm2 restart --env dev with different port number.
{
"apps": [
{
"name": "MyWebsite",
"script": "server.js",
"env": {
"NODE_ENV": "production",
"PORT": 8080
},
"env_dev": {
"NODE_ENV": "development",
"PORT": 3000
}
}
]
}

How to include nodemon in pm2

I am using pm2 for my application, I need to have a fast refresh, we can achieve this with nodemon. But when I tried to pass inside the node_args I am not able to get.
Approach 1
node_args: ["--inspect", "nodemon"]
Error
Can't find module nodemon
Tried Solution
Installed nodemon as globally
Installed nodemon as devDependencies
Approach 2
node_args: ["--inspect", "--nodemon"]
Error
node: bad option: --nodemon
process.json
{
"apps": [
{
"name": "app_local",
"script": "dist/src/app.js",
"watch": "dist/src/**/*.js",
"error_file": "logs/error.log",
"out_file": "logs/app.log",
"merge_logs": true,
"max_memory_restart": "1G",
"exec_mode": "fork",
"instances": "1",
"min_uptime": "2s",
"node_args": ["--inspect", "--nodemon"],
"env": { "NODE_ENV": "development", "PORT": "4000" }
},
{
"name": "app_staging_cluster",
"script": "dist/src/app.js",
"watch": "dist/src/**/*.js",
"error_file": "logs/error.log",
"out_file": "logs/app.log",
"merge_logs": true,
"max_memory_restart": "1G",
"exec_mode": "cluster",
"instances": "4",
"min_uptime": "2s",
"env": { "NODE_ENV": "development", "PORT": "3000" }
},
{
"name": "app_prod",
"script": "dist/src/app.js",
"watch": "dist/src/app.js",
"error_file": "NULL",
"out_file": "NULL",
"merge_logs": true,
"max_memory_restart": "1G",
"exec_mode": "fork",
"instances": "1",
"min_uptime": "2s",
"env": { "NODE_ENV": "production", "PORT": "8080" }
}
]
}
package.json
"start": "NODE_ENV=development gulp build && pm2 start process.json --only app_local --no-daemon",
Even added the watch flag in process.json but not working. anything to updated in the json file.
That's why thought of using nodemon.
as stated here, I don't think you really need nodemon AND pm2... I use pm2 for personal projects in 'production' but nodemon when developing locally...
Hope this answer helps guide you further!
Is PM2 meant to be used during development process?

I am using pm2 and ecosystem file for its configuration. Auto restart apps on file change is not working

I am trying to debug node app using below config(launch.json) in VS code:
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"port": 9229
},
and the command to run is : pm2 start ecosystem.config.js --only development.
My ecosystem config file looks like for development mode:
apps: [
{
name: "development",
script: "./app.js",
node_args : ["--inspect-brk"],
watch: true,
ignore_watch : ["node_modules", "logs"],
watch_options: {
"followSymlinks": false
},
.....
.....
.....
env: {
NODE_ENV: "development"
}
}]
My changes are not reflecting even though , I am using watch: true.
With the below command in package.json file , I am able to debug my app. "debug": "pm2 start ./app.js --node-args=--inspect-brk --watch"

VS Code nodejs distant debugging: breakpoints are getting ignored

I'm trying to set up a distant debug with VS Code on a node.js (7.1.0) project.
I launch my dev app with pm2:
{
"apps": [{
"name": "my-app-dev",
"script": "app.js",
"watch": true,
"node_args": ["--inspect=9229", "--nolazy"],
"ignore_watch": ["assets/images/", ".tmp/", ".git/"],
"watch_options": {
"usePolling": true
}
}]
}
this is the .vscode/launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node2",
"request": "attach",
"name": "attach",
"port": 9229,
"address": "my.app.local",
"restart": true,
"diagnosticLogging": true,
"sourceMaps": false
}
]
}
When I start the debugger I successfully get attached Debugger attached.
The problem is my breakpoints are getting ignored: Unverified breakpoint
finally found a solution by adding
"localRoot": "${workspaceRoot}/api",
"remoteRoot": "/srv/www/my-app/api"
to my launch.json.
If someone can explains why its needed :o

Running foreman start through visual studio code launch.json

Currently, I am running my solution by typing foreman start into the command line and that is working fine. I'm trying to debug my code using visual studio code. In order to do so, I have created a launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/package.json",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "start",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
]
}
I have created a tasks.json file to try to start the program from that point:
{
"version": "0.1.0",
"command": "start",
"isShellCommand": true,
"args": [
"--no-color"
],
"tasks": [
{
"taskName": "test",
"args": [],
"isTestCommand": true
},
{
"suppressTaskName": true,
"taskName": "start",
"args": [
"foreman",
"start"
],
"isBuildCommand": true
}
]
}
When I run foreman start normally, I see this output:
$ foreman start
12:00:59 web.1 | started with pid 22641
12:00:59 workers.1 | started with pid 22642
12:00:59 intermediary.1 | started with pid 22643
12:01:00 web.1 | [INFO] Node app is running at localhost: 3777
If I debug in this current state, the output from the console is:
Failed to launch external program start --no-color.
spawn start ENOENT
If I change my program to point to gulp:
"program": "${workspaceRoot}/node_modules/.bin/gulp",
It gives me something a little more promising, but because it isn't foreman, it doesn't run everything I need.
node --debug-brk=16751 --nolazy node_modules/.bin/gulp
Debugger listening on port 16751
[16:23:17] Using gulpfile ~/Git/backend/gulpfile.js
[16:23:17] Starting 'watch'...
[16:23:18] Finished 'watch' after 125 ms
[16:23:18] Starting 'default'...
[16:23:18] Finished 'default' after 13 μs
Does anyone know how to debug foreman start from visual studio code?
This will run foreman start as shell command. Press F1, type Run Task, Enter and select development task.
tasks.json
{
"version": "0.1.0",
"command": "foreman",
"isShellCommand": true,
"tasks": [
{
"suppressTaskName": true,
"taskName": "development",
"args": [
"start"
],
"isWatching": true
}
]
}
If you want to debug a web application you should take a look at vscode-chrome-debug if it is a node application you have to set the entry point of your app as program in launch.json "program": "${workspaceRoot}/app.js"

Resources