How to make PM2 reload a node_modules sub-folder - node.js

I have a NodeJS app my-project, that has a module my-module as a dependency. Currently, whenever I make changes to my-module, I have to ctrl + C to quit PM2, and type pm2 start ... to start up my-project again.
This is my pm2 config file. As you can see, watch is already enabled; However, unless I quit and restart, the changes are not reflected.
{
"name": "my-project",
"script": "dist/index.js",
"watch": ["src", "../my-module/src"],
"ignore_watch": [
"node_modules"
],
"env": {
"NODE_ENV": "development",
"PORT": 8000
},
"watch_options": {
"followSymlinks": false
}
}
Is there a way to make it so that if I make changes to my-modules, they are reflected by the watch option, rather than manual restart?

Related

Reduce Loopback4 build times in development (build + restart dev server)

I am using Loopback4 to develop api in Node.
I was using the instruction given to build and watch with nodemon
It worked, but it was getting slow, like about 15 seconds in my case.
I search for other other solution and came up with idea of using Turborepo and the nodemon solution.
I wanted to know if there are better solutions or any issues with it
Gist of the solution
run build in watch mode and restart the dev server if js files in dist folder change
use Turbo repo to run these build and restart server tasks
Steps
Setup build and watch with nodemon as described in the thread
you should have something like this in the script
"scripts": {
"dev:server:watch": "nodemon server.js"
}
// watch src folder
// ignore dist
// ext only ts files
// npm start to start the dev server on any changes to the files
"nodemonConfig": {
"verbose": true,
"watch": [
"src/"
],
"ignore": [
"dist/*"
],
"ext": "ts",
"exec": "npm start"
}
Install turbo build system
yarn add turbo --dev
Update nodemon config to restart server on changes in js files in dist folder after build step
"nodemonConfig": {
"verbose": true,
"watch": [
"./dist/"
],
"ext": "js",
"exec": "yarn run start"
},
Add turbo.json
{
"pipeline": {
"dev": {
"dependsOn": ["build:watch", "dev:server:watch"]
},
"build:watch": {
"outputs": [
".dist/**"
]
},
"lint": {
"outputs": []
}
}
}
Add dev script in package.json "scripts"
"dev": "turbo run dev",
Run
yarn run dev
This seems to have reduced the build times to about 3 seconds
Can anyone confirm if this is an acceptable solution, points out any issues
Thanks

What could be affecting my Node.JS environment that does not allow me to debug my JS with babel-node?

I have a Node JS server written with ES6 features and use Babel to transpile the code for production. The code itself complies and works fine. I am also able to run my "dev" server and test it locally with this command:
npm run dev
which runs this command inside my package.json:
"dev": "nodemon --exec babel-node ./src/server.js"
Pretty standard so far.
I am having issues with debugging my code so I can use breakpoints. Here's the launch script in my VS Code launch.json file:
{
"name": "Debug",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/src/server.js",
"stopOnEntry": false,
"sourceMaps": true,
"args": [],
"preLaunchTask": null,
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
"runtimeArgs": ["--no-lazy"],
"env": {
"NODE_ENV": "development"
},
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"<node_internals>/**/*.js"
]
}
And my .babelrc file:
{
"presets": [
[
"#babel/env",
{
"targets": {
"node": "current"
}
}
]
],
"env": {
"development": {
"sourceMaps": "inline",
"retainLines": true
}
},
"comments": true,
"plugins": [
"#babel/plugin-proposal-class-properties",
"#babel/plugin-proposal-object-rest-spread"
]
}
When I try to enter debug mode I get this execption thrown right away:
Exception has occurred: Error: Cannot find module 'kexec'
Require stack:
- F:\Dev\Web Development\****\dev\server\node_modules\#babel\node\lib\babel-node.js
- F:\Dev\Web Development\****\dev\server\node_modules\#babel\node\bin\babel-node.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
at Function.Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at F:\Dev\Web Development\****\dev\server\node_modules\#babel\node\lib\babel-node.js:65:68
at processTicksAndRejections (node:internal/process/task_queues:96:5)
This is where is gets really odd. I pulled the same sourcecode to my laptop and debugging worked just fine. I also spun up a sandbox virtual machine on my Windows 10, did a clean install of Node + VS Code on it, and it worked perfectly there too.
There's something in my current environment that is causing this issue and I cannot figure it out. I've been trying to solve this issue for a few days now with no success.
Here are the steps I have already taken:
Upgrade and downgrade versions of Node + NPM and retry using
different versions
Delete node_modules and reinstall with "npm install" (and using npm ci)
Completely uninstall Node and do a fresh install
Removed user and system environemnt variables before the fresh install
Manually delete all NPM caches from %AppData% folder
I also want to point out that when I used a different launch script that is attached to a process ID, I was able to debug the code, but I am trying to streamline this process instead of having to choose the process each time.
This method does work:
npm run dev to start the dev server, which runs this code:
"dev": "nodemon --exec babel-node ./src/server.js"
I then run the debugger and attach it to the running process. Here's the launch script for that:
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
}
If kexec is used by Babel, isn't it already supposed to be installed as a dependecy? I couldn't find it anywhere in my modules folder though.
I also tried installing kexec separately but was receiving a lot of node-gyp errors which I tried fixing by reinstalling all of Node build tools using multiple different methods. None of these actions also fixed the issue.
Any ideas or support would tremendously help at this point.

VS Code not hitting breakpoints for Node app running in Docker Container

Summary
I'm running a node app inside a docker container can't get the VS code debugger to hit breakpoints.
Docker Setup
The docker container exposes port 5859. Inside the container the node app is ran with this command:
nodemon -L --watch src --exec babel-node src/server.js -- --inspect=0.0.0.0:5859 --nolazy
It reports that the debugger is listening:
[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /app/src/**/*
[nodemon] starting `babel-node src/server.js --inspect=0.0.0.0:5859 --nolazy`
Debugger listening on ws://0.0.0.0:5859/5939f6b6-5ade-4ce5-9694-7df5f5b8385b
For help, see: https://nodejs.org/en/docs/inspector
VS Code Setup
And when I fire up the debug profile in VS Code it appears to attach. Below is line from the logs of the running docker container.
However, no breakpoints are hit when I set them. Is this a babel-node issue? Is there any suggested path forward to get node debugging to work with babel-node?
My VS Code debug config:
{
"type": "node",
"request": "attach",
"name": "Docker: GraphQL",
"port": 5859,
"protocol": "inspector",
"restart": true,
"remoteRoot": "/app",
"localRoot": "${workspaceFolder}"
}
I was not able to get this to work with nodemon, but modifying my .babelrc file to include inline source maps triggered VS code to hit the breakpoints I set. My .babelrc file looks like this:
{
"env": {
"production": {
"presets": [
["es2015", {"modules": false}],
"stage-1"
]
},
"development": {
"presets": [
["es2015"],
"stage-1"
],
"sourceMaps": "inline",
"retainLines": true
} }
}
And the corresponding script that docker calls in package.json. Port 5859 is exposed in the docker-compose file.
"start:docker": "babel-node src/server.js --inspect=0.0.0.0:5859 --nolazy",
For anyone else arriving via a search like me. In my case changing ${workspaceRoot} to ${workspaceFolder} in my vs code launch.json file worked to get a previously working project debuggable again.

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.

VSCode editor - Restart NodeJs server when file is changed

I am using Visual Studio Code as my editor for NodeJS project.
Currently I need to manually restart server when I change files in my project.
Is there any plugin or configuration change in VSCode that can restart NodeJS server automatically when I make change in files.
You can now use Nodemon with VS Code to achieve this. I tested the Nodemon support for VS Code today and it worked well for me. Below are my VS Code details.
Version: 1.9.1
Commit: f9d0c687ff2ea7aabd85fb9a43129117c0ecf519
Date: 2017-02-09T00:26:45.394Z
Shell: 1.4.6
Renderer: 53.0.2785.143
Node: 6.5.0
I installed Nodemon globally npm install -g nodemon and created VS Code launch configuration as below
{
"name": "Nodemon Launch Server",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--debug=5858"
],
"program": "${workspaceRoot}/server.js",
"restart": true,
"port": 5858,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Reference: https://code.visualstudio.com/docs/editor/node-debugging#_restarting-debug-sessions-automatically-when-source-is-edited
You can also install nodemon locally npm install nodemon --save-dev.
And the following example of configurations of VS Code launch.json:
[
{
"name": "Nodemon",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/src/server/index.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
Restarting the debugger automatically after editing our Application files :
Add debugger configuration in Vscode lunch program for nodejs like below screen-shot.
Add two lines in below file path:
.vscode/launch.json
"runtimeExecutable": "nodemon",
"restart":true
Assuming the you have installed nodemon globally
npm install nodemon -g
More information follow the official document link: https://code.visualstudio.com/docs/nodejs/nodejs-debugging
Use pm2 to watch your code and restart automatically
npm install pm2 -g
npm install pm2
process.json
{
name : "App",
script : "app.js",
watch : true,
}
You can find the demo #
https://github.com/sivasankars/jade-title-rendering
To add to Siva's comments
This would go to the ecosystem.config.js with the new pm2 version
module.exports = {
apps : [{
**name: 'App',
script: 'app.js',
watch: false,**
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}],
deploy : {
production : {
user : 'node',
host : '212.83.163.1',
ref : 'origin/master',
repo : 'git#github.com:repo.git',
path : '/var/www/production',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
}
}
};
Here is what worked for me to run an Express server:
{
"name": "Nodemon Launch Server",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--inspect-brk"
],
"program": "${workspaceFolder}/bin/www",
"restart": true,
// "console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
For me the Oleksandr's answer didn't work.
Instead of
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
the below should work.
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
Also note that making sure the node program file should be on the current or project directory, for example, nodemon-ning ./../file.js may not work but nodemon does not restart the program when its file change depending on the nodemon version or the environment. Your script file should be located on e.g. ./file.js

Resources