Getting error when running nodemon server.js - node.js

I'm using Visual Studio Code to run ExpressJs in debug mode.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\nodemon server.js"
}
]
}
I'm getting the following error when I start debugging.
If I run nodemon server.js in the terminal it works. The above error only appears when using Visual Studio Code debugging. I have tried the the following:
Remove node_modules and reinstall node_modules
Reinstall nodemon globally

You need to update this code line
"program": "${workspaceFolder}\\nodemon server.js"
to
"program": "nodemon ${workspaceFolder}\server.js"

I ended up using Visual Studio Code "JavaScript Debug Terminal". In the terminal I run command nodemon server.js and it works.

Related

How to debug nodeJS (ExpressJS) app in visual studio code?

I have tried many different solutions available online but nothing works in my case,
I am trying to debug a nodeJS app while its running, invoking the API through UI/postman
My launch.json: took guide from here
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"skipFiles": [
"<node_internals>/**"
]
}
After starting local server, when I start debugger, it ask to pick a process, I select the one ending with --exec babel-node server.js it attaches successfully but deosn't load my project scripts, only node_modules, eval and node_internal.
In my code if I put break point, I see this error "Break point set but not yet bound"
My package.json start sctipt:
"scripts": {
"start": "nodemon --exec babel-node server.js"
}
My code is in ES6, I start the server though a shell script which first set some environment then do npm start
My .bablerc
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
Here is my launch.json and I run nodejs with express and it work fine.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"args": ["--env","local"]
}
]
}
Turns out I was picking wrong process ID.
When we start the debugger, it shows us the list of running processes to select with which debugger will be attached.
I picked the one ending with _babel-node server.js instead of --exec babel-node server.js. Doing this loaded all my script and started working as expected.

Can't get VSCode debbuging to work with my NodeJs app

I'm having trying to debug my app on Visual Studio Code. I have the following config on my package.json:
"scripts": {
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
"start": "npm run build && node --inspect=12345 dist/app.js"
}
Im using ES6 on my Node app, that's why it is kinda messy my build config.
When I run npm start everything works fine, I can use my app.
Now to try to debug it, I have set the following launch configurations:
"configurations": [
{
"type": "node",
"name": "Attach to Remote",
"request": "attach",
"port": 12345
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\app.js"
}
]
Both of them ""work"": VS Code switch to "debug mode" but I can't hit any breakpoints. They all get grayed out:
I have tried to fix using this answer, but couldn't get it to work...
Any help?
Thanks in advance!
I am using VS Code v 1.28.2 and I'm able to debug both ways.
1) With the built in debuger ( Menu -> Debug -> Start Debuging)
2) starting the application with node inspect index.js. In that case you have to declare breakpoints in your code with the debugger; keyword. Then, when in debug-mode and stoped in a breakpoint, you continue execution typing cont in the command line.
hope it helps
I found out I was just missing the --source-maps from my babel-cli command... -.-
After adding it, VSCode is able to find the breakpoints.
So basically the solution was:
Add --source-maps to my build command:
"scripts": {
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files --source-maps",
"start": "npm run build && node --inspect=12345 dist/app.js"
}
And I configured a launch as follows:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\app.js",
"preLaunchTask": "npm: build"
}
]
Hope it helps someone!

Alternative debug output for node/vscode, while building a terminal based visualization

i'm developing a terminal based app with Node, and i'm using the terminal as output of a Blessed.js visualization.
So, i can't use the terminal to console.log thing for debug.
Does node offer an alternative debug output? I'm using VSCode.
Thanks!
In package.json add in scripts section:
"scripts": {
"debug": "node --nolazy --inspect-brk=9229 main.js"
},
In vscode launch.json add:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
}
]
}
Now:
First run blessed app with npm run debug
In 2nd step run vscode debugger with F5
You will see both blessed app interface and debugger in vscode.
Works for me ;)

VSCode debugger wont attach with Launch via NPM

Im sure this is super simple but I cant seem to get the debugger going with the Launch via NPM vscode template. I have a really simple hello world with an npm script to run the app.
If I run Launch Program (the config that uses just node) everything works perfectly, however if I use Launch via NPM I get
/Users/luke/.nvm/versions/node/v6.5.0/bin/npm --debug-brk=3837 run-script runit
hello-world#1.0.0 runit /Users/luke/source/playground/js/hello-world
node index.js
hello world
And no breakpoints are hit. (Ive also tried with and without "protocol":"legacy")
What am I doing wrong, all the online examples suggest that this should justworkTM.
package.json
{
"name": "hello-world",
"version": "1.0.0",
"scripts": {
"runit": "node index.js"
}
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"protocol":"legacy",
"runtimeArgs": [
"run-script",
"runit"
]
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.js"
}
]
}
index.js
console.log('hello world');//with a breakpoint set here
Ok I worked it out...
Launch via NPM requires you to add some extra args into the actual NPM script:
{
"name": "hello-world",
"version": "1.0.0",
"scripts": {
"runit": "node --nolazy --debug-brk=5858 index.js"
}
}

Can Visual Studio Code be configured to launch with nodemon

I have installed nodemon as a global package in my system.
It works when I executed nodemon in cmd.
But when I am using vscode with this launch.json file, vscode throws this exception:
request launch: runtime executable XXX\XXX\XXX\XXX\nodemon does not exists
the launch.json is:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "app.js",
"stopOnEntry": false,
"args": [],
"cwd": ".",
"runtimeExecutable": nodemon,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"preLaunchTask": "",
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
when I erase the nodemin in runtimeExecutable it runs perfectly with node
First, install nodemon as a dev dependency:
npm install --save-dev nodemon
For newer versions of VS Code set up your .vscode/launch.json file like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}]
}
The most important pieces are the runtimeExecutable property that points to the nodemon script and the program property that points to your entry point script.
If you use an older VS Code (which you shouldn't), try this launch configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch with nodemon",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/nodemon/bin/nodemon.js",
"args": ["${workspaceRoot}/app.js"],
"runtimeArgs": ["--nolazy"]
}
]
}
The most important pieces are the program property that points to the nodemon script and the args property that points to your normal entry point script.
I couldn't get #AdrianT's answer working with the debugger attached. It seems like there's a newer built-in supported way to do this:
Open the Launch Configuration dropdown and select "Add configuration..."
Select "Node.js: Nodemon Setup"
It will add something like this to your launch.json:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceRoot}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Make sure your "program" setting is your correct entry point script.
You need to install nodemon globally to get this to work (npm install -g nodemon) (as per the documentation)
Your app now runs and you can set breakpoints which will be hit and the console logs to the integrated terminal window.
Note that terminating the debug session only terminates the program to debug, not nodemon itself. To terminate nodemon, press Control-C in the integrated terminal.
In Visual studio code create a launch config:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"restart": true
}
run nodemon from the command line: nodemon --debug server.js
Now 'Attach' from VC and vuala.
Attaching is definitely an easy option. In order to make sure that your code breaks, make sure you run nodemon with --inspect-brk (node 8+), e.g.:
nodemon --inspect-brk src/app.js
After launching nodemon will log the port open for debug connections:
Debugger listening on ws://127.0.0.1:9229/someUUID
You can take that port in order to build your launch config which is quite simple:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"restart": true
},
I tried the solutions Adrian and Mathew suggested. They seem to work perfectly if your are on macOS (maybe also on Linux). On Windows, maybe Mathew's solution is more stable. Combining both to have a solution that could be compatible with both macOS, Windows and Linux, and makes sure that we don't face with errors like "PATH not found", I found that using globally installed nodemon for debugging seems to be much more stable.
Install nodemon globally (if you haven't done it before) npm i -g nodemon
Add the following to the .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug App_Name",
"skipFiles": [
"./path/of/file/to/skip/when/debugging"
],
"program": "app.js",
"restart": true,
"runtimeExecutable": "nodemon",
"console": "integratedTerminal"
}
]
}
We, of course, can still install nodemon locally and run it while developing.
What worked for me without global installs and using typescript:
{
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"name": "nodemon",
"program": "${workspaceFolder}/src/index.ts",
"request": "launch",
"restart": true,
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
"type": "pwa-node",
"args": ["--config", "nodemon.json"] //remove --config if you don't have one
}
In order to don't have problems with ts-node, in nodemon.json I added it:
{
"execMap": {
"ts": "npx ts-node"
}
}
No, currently it can't. But I managed to get this somewhat working using nodemon. I start it from Grunt . But an equivalent command line should do the same.
EDIT: After an evening of testing I can say that below approach is still somewhat flakey :S, attaching fails intermittedly and sometimes breakpoints are ignored.
EDIT2: You can also specify an non default debug port in Gruntfile using ['--debug-brk=5860'] for nodeArgs. I've been also advised to use --debug-brk instead of --debug. Perhaps this will remove the current flakeyness. I'll come back and mention here if it helps (I've currently switched project).
In case this might help anyone it's working with below settings in Current VS Code version (e.g. v0.10.6) on Windows 10. But it'll probably work on Mac too (I might check later). But note that I sometimes have to trigger a rebuild by changing+saving a file before the debugger picks it up.
/.vscode/launch.json
{
"configurations": [{
"name": "Launch",
"outDir": null
},{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}]
}
/Gruntfile.js
nodemon : {
dev : {
script : 'launcher.js'
},
options : {
ignore : ['node_modules/**', 'Gruntfile.js'],
nodeArgs: ['--debug'],
env : { PORT : '4123'
}
}
}
I guess the debug port 5858 is the default since it's not specified here (note it ís in launch.json above.)
https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
The above link helped me to successfully debug nodemon + express app. The steps are well explained there.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector",
}
]
}
npm script
"dev-server": "nodemon ***--inspect*** server.js"
Steps:
Run the server, using npm script. Please note --inspect arg in
the script
Start visual code debugger, A prompt will be shown to
select the node server process
select the node server process
Now you should be able to debug.
if it did not help you, then please have a look at the official doc, the config options are explained there.
https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools
This will let you run it on the file currently open in the editor WITHOUT installing nodemon as a dependency. This makes it convenient to keep in a template project.
The cwd and program are set so that the working directory is the one containing the file, and the program is the filename without a path. This makes it compatible with monorepos because it will then search back up the file tree for the correct tsconfig.json, package.json, node_modules, etc.
E.g. if the currently opened file is /path/to/some-file.ts, this is equivalent to running in the shell like:
cd /path/to
npx -y nodemon some-file.ts
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "npx",
"runtimeArgs": ["-y", "nodemon"],
"program": "${file}",
"cwd": "${fileDirname}",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Yes you can! As of a recent update you can attach the debugger to a running Nodemon process. This page has more information. Search for nodemon on the page to see the instructions.
I use the Node Exec plugin. It allows you to run and stop a node app in vcs by pressing F8 and F9 (applies on open file in editor). This could help as a (temporary) workaround.
Nodemon as local dependency
I also could not get #Adrian T's answer to work right away. But it is only a small detail, that has to be changed to make it work. So here we go:
create a launch.json file in a top-level .vscode folder
open the file in VS Code
use the build in button Add Configuration - that will be rendered in the editor - to add a config for Node.js: Nodemon Setup
in the generated config change the key runtimeExecutable:
"program": "${workspaceFolder}/app.js",
// diff from Adrian T
-- "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
++ "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
You are now using the executables from your local dependencies (see this SO thread)
There is also an answer by Martin from earlier underneath this answer (just to be correct):
If you don't like having to run a global nodemon you can also install nodemon using npm and then set "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon", – Martin Feb 22 '18 at 22:25
Nodemon as global dependency
"runtimeExecutable": "nodemon", will only work if the executable of nodemon itself is part of the environment variable PATH (not `%appdata% or the like thereof). As this is mostly not the case, you would need to specify the absolute path (see the docs here and here).
For anyone trying to set up nodemon with an express-generator created ExpressJS project on windows, this is what worked for me when nodemon is installed as a local dev dependency (e.g. npm install nodemon --save-dev)
{
"type": "node",
"request": "launch",
"name": "Launch with Nodemon",
"runtimeExecutable": "node",
"runtimeArgs": ["${workspaceFolder}/node_modules/nodemon/bin/nodemon.js"],
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\bin\\www",
"env" : {
"DEBUG": "myapp:server"
}
}
No need to do anything,
Just open Powershell as administrator and write
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
and then enter A
1. Install nodemon as a dev dependency.
npm install -D -E nodemon
Options:
-D option: To save as dev dependency
-E option: To install exact version
2. Add a nodemon.json file in the root of the project with the following content.
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": {
"js": "node --harmony"
},
"watch": ["src/", ".env"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json",
"delay": 500
}
For more information, see the official documentation here.
3. Add a script to package.json.
npm pkg set scripts.dev="nodemon --config nodemon.json"
4. Add launch config in .vscode/launch.json.
Content:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run Nodemon",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "dev"],
"envFile": "${workspaceFolder}/.env",
}
]
}
To see documentation here.
The envFile option is optional. I use it because I am using dotenv.
It works fine for me, in the main Terminal.
Do not use VS code terminal, use the main system terminal.

Resources