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

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"

Related

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?

VSCode debug configuration for Next.js Frontend with Node.js backend

In my project directory I have a frontend in next.js and a backend in Node.js it looks as follows:
I now would like to debug the program, so both backend and frontend have to run, and I don't know how to create the corrent debug launch.json configuration which I have in my top-level project, to launch and debug both programs.
{
"version": "0.2.0",
"compounds": [
{
"name": "Client+Server",
"configurations": [ "DCBACKEND", "DCFRONTEND" ]
}
],
"configurations": [
{
"type": "node",
"request": "launch",
"name": "DCBACKEND",
"cwd" : "${workspaceFolder}\\dcbackend",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "start"
]
},
{
"type": "node",
"request": "launch",
"name": "DCFRONTEND",
"cwd" : "${workspaceFolder}\\dcfrontend",
"program": "${workspaceFolder}\\dcfrontend\\pages\\index.js",
"runtimeExecutable": "next",
"runtimeArgs": [
"--inspect"
]
}
]
}
EDIT: Now, I get the following error:
My start script for my backend is:
"scripts": {
"start": "node index.js -p 7766"
},
so there is something wrong with the port maybe? Also, it runs another script for debug in cmd-line:
C:\Program Files\nodejs\npm.cmd run-script start --inspect-brk=39777
I don't know what it has to do with --inspect-brk=39777, do I need that

VS Code settings to launch frontend and backend for debug

I'm trying to get a simple react frontend and nodejs backend up and running and debuggable in vs code. I'm using a compound launch configuration to launch 'client' and 'server' together. The nodejs backend is started automatically for me, but I always have to do an npm start in the console for the client for it to connect. All the tutorials I've seen suggest that this step has to happen before running the debug configuration in vs code. Is it not possible for vs code to start the frontend just like it does for the nodejs backend.
This is what my launch.json looks like:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
{
"name": "Client+Server",
"configurations": [ "Server", "Client" ]
}
],
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server/server.js"
},
{
"type": "chrome",
"request": "launch",
"name": "Client",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}/client/src/index.js"
}
]
}
I had some trouble launching the debug session from beginning so I decided to start the nodejs server then attach the debuggers.
I found this configuration working for me.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach Server",
"restart": true,
"port": 9000
}, {
"type": "chrome",
"request": "launch",
"name": "Launch Client",
"port": 9001,
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}/dist",
"sourceMaps": true
}
],
"compounds": [
{
"name": "Attach Client+Server",
"configurations": ["Attach Server", "Launch Client"]
}
]
}
And here are some of the scripts I used for package.json.
{
"scripts": {
"prestart": "rollup -c -w",
"start": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index"
},
}
I had to use nodemon to detect changes and restart the node server.
But if your React application need to be started separately from your node application then I suggest using something like http-server to start the local server for your React app. And then use concurrently to start both applications concurrently.
Your package.json may look like the following:
{
"scripts": {
"prestart": "rollup -c -w",
"start:client": "http-server ./dist/client/",
"start:server": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index",
"serve": "concurrently \"npm:start:client\" \"npm:start:server\""
},
}
Resource VS Code debug configuration: https://code.visualstudio.com/Docs/editor/debugging
The reason that the client requires an npm start is because you are referring to http://localhost:3000
npm start will actually run a mini web server to serve up your html files on http://localhost:3000
The other way is to use something like http-server on your src, which will have the same effect

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