How to include nodemon in pm2 - package.json

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?

Related

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"

Debug using nodemon + babel-node in VSCode using WSL

I'm trying to debug my node + babel project with VScode without success.
I read a lot of answers no one fixed my problem.
How should launch.json look alike?
I want to be able to attach the debugger to a running process and launch the program.
Package.json
"build-babel": "npx babel src -d dist",
"start": "node dist/server.js",
"dev-start": "nodemon --inspect-brk --exec babel-node ./src/server.js",
First try from:
Can Visual Studio Code be configured to launch with nodemon
launch.json
{
"type": "node",
"request": "launch",
"name": "Nodemon",
"runtimeExecutable": "${workspaceRoot}/node_modules/nodemon/bin/nodemon.js",
"args": [
"${workspaceRoot}/src/server.js"
],
"restart": true,
"protocol": "inspector",
"stopOnEntry": true,
},
{
"type": "node",
"request": "attach",
"name": "Attach to app",
"port": 9229,
"address": "localhost",
"sourceMaps": true,
"smartStep": true,
"restart": true
},
update
I also tried this:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/src/server.js",
"restart": true,
"console": "integratedTerminal",
"port": 9229,
"args": ["--exec", "babel-node", "--babel-preset-es2015"],
"internalConsoleOptions": "neverOpen"
}
run it when there is a server process running from a terminal: attached but the breakpoints don't got hit "set but not yet bound"
The launch process result error:
Cannot connect to runtime process- reason: connot connect to the target: connect econnrefused 127.0.0.1:9229
Thank you

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

pm2 launching from a JSON not working

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?

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