How to merge env variables for PM2 commands along with existing env variables? - node.js

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
}
}
]
}

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?

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"

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?

Resources