Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 279
Mickel Sierra Avatar asked Dec 24 '15 09:12

Mickel Sierra


People also ask

Why Nodemon is not working in VS Code?

If you get this error: nodemon: command not found , it means nodemon was not installed properly. Try using this: sudo npm install -g --force nodemon , which worked in my case. If you are using Windows, npm i -g nodemon should work. Edit and save your app to see nodemon in action.


2 Answers

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.

like image 180
Adrian Theodorescu Avatar answered Sep 19 '22 12:09

Adrian Theodorescu


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:

  1. Open the Launch Configuration dropdown and select "Add configuration..."
  2. 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.

like image 34
Mathew Avatar answered Sep 21 '22 12:09

Mathew