52

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

Mickel Sierra
  • 705
  • 1
  • 5
  • 11
  • 1
    VS Code expects an absolute path for the "runtimeExecutable". So on OS X using "/usr/local/bin/nodemon" will make the launch config work. However, at the end of the debug session VS Code will kill nodemon which is not the intent of using nodemon in the first place. That's why the answer below is a much better approach. – Andre Weinand Jan 19 '16 at 10:55
  • This helped me :) => https://github.com/Microsoft/vscode-recipes/tree/master/nodemon – Rohith K P Mar 12 '19 at 17:19

16 Answers16

79

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.

Adrian Theodorescu
  • 11,664
  • 2
  • 23
  • 30
  • Any idea how to pass in parameters to nodemon, such as `--watch /server/**.ts --exec ts-node server/server.ts` for example? Typically it's just one big line in either package.json scripts or cmd line. `nodemon --watch server/**/*.ts --ignore server/**/*.spec.ts --exec ts-node server/server.ts` – Mark Pieszak - Trilon.io Feb 07 '17 at 15:05
  • Haven't tried it myself, but you should be able to pass all the arguments you need in the "args" array. For the example you mentioned, try using `"args": ["--watch", "/server/**.ts", "--ignore", "server/**/*.spec.ts", "--exec", "ts-node", "server/server.ts"]` – Adrian Theodorescu Feb 07 '17 at 15:54
  • 1
    any idea how to do this with babel-node/es6? this is the actual nodemon command I want to run: `nodemon src/shim-host/index.js --exec babel-node --babel-preset-es2015` but it's not working when I add the relevant file to `program`, and the nodemon arguments to `runtimeArgs` – devdropper87 Jul 18 '17 at 18:03
  • @devdropper87: You need to change "args", not "runtimeArgs". Try this: `"args": ["${workspaceRoot}/src/shim-host/index.js", "--exec", "babel-node", "--babel-preset-es2015"]` – Adrian Theodorescu Jul 18 '17 at 18:31
  • @AdrianT I tried that, here's the nodemon command generated (which then crashes): `[nodemon] starting babel-node ${workspaceRoot}/src/shim-host/index.js --babel-preset-es2015` – devdropper87 Jul 18 '17 at 19:11
  • Ah I see thanks, but then how would `nodemon` be leveraged in that case? – devdropper87 Jul 18 '17 at 20:18
  • @devdropper87: You are correct, I probably need another coffee. Actually the answer in my first comment was correct. I will delete the second (wrong) answer. Make sure you are using the latest version of VS Code and specify `"version": "0.2.0"` in your launch config. I just updated the snippet in the answer to include the `version` property. – Adrian Theodorescu Jul 18 '17 at 20:57
  • Can anyone provide a working solution to this showing the args? I'm running into the same issue. – mjbates7 Jul 21 '17 at 16:00
  • This seems to not work- running VSCode 1.30.2. [Matthew's answer](https://stackoverflow.com/a/46445633/2483138) worked for me – ABMagil Feb 04 '19 at 21:16
  • @ABMagil I updated the answer to include the correct configuration for newer versions of VS Code. I also made sure it works with a locally installed nodemon, as opposed to a global install, as per the default VS Code config. – Adrian Theodorescu Feb 05 '19 at 21:05
  • This should really be how VSCode generates the configuration based on the system. If nodemon is in the workspaces node_modules folder than use one from this otherwise check for a global installed one generate the config to use that. – philk Oct 11 '19 at 21:17
49

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.

Mathew
  • 4,297
  • 4
  • 29
  • 38
  • 4
    This is should be the accepted answer as it's the correct way to do this now. – Rafael del Rio Nov 09 '17 at 11:10
  • 9
    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",` – xyhhx Feb 22 '18 at 22:25
  • what about flags, do they go in runtimeExecutable – Muhammad Umer Nov 11 '19 at 16:36
  • If this helps anyone, make sure that you pass correct JS file in `program` parameter. In my case it was `\bin\www.js` and not `app.js`. – dotNET Feb 09 '22 at 11:16
14

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.

Yevgeni
  • 1,533
  • 17
  • 32
5

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
},
Fred
  • 3,324
  • 1
  • 19
  • 29
2

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.

Hoang
  • 361
  • 2
  • 4
2

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"
    }
}
João Paulo
  • 6,300
  • 4
  • 51
  • 80
1

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.)

Bart
  • 5,065
  • 1
  • 35
  • 43
1

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:

  1. Run the server, using npm script. Please note --inspect arg in the script
  2. Start visual code debugger, A prompt will be shown to select the node server process
  3. 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

Rohith K P
  • 3,233
  • 22
  • 28
1

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"
    }
}
rovyko
  • 4,068
  • 5
  • 32
  • 44
1

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"
    }
  ]
}
Eric Haynes
  • 5,126
  • 2
  • 29
  • 36
0

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.

Aron
  • 8,696
  • 6
  • 33
  • 59
  • 1
    There is nothing on https://code.visualstudio.com/Docs/editor/debugging mentioning nodemon... – Tomas Voracek Jan 30 '17 at 18:10
  • There was in June 2016: https://web.archive.org/web/20160630150344/https://code.visualstudio.com/Docs/editor/debugging – Aron Oct 01 '17 at 22:08
0

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.

KiSa87
  • 23
  • 6
0

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:

  1. create a launch.json file in a top-level .vscode folder
  2. open the file in VS Code
  3. use the build in button Add Configuration - that will be rendered in the editor - to add a config for Node.js: Nodemon Setup
  4. 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).

flipcc
  • 85
  • 6
0

No need to do anything, Just open Powershell as administrator and write

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

and then enter A

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

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.

-1

It works fine for me, in the main Terminal. Do not use VS code terminal, use the main system terminal.

Raam C
  • 1
  • 3
    A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it. – Tyler2P Jan 15 '22 at 15:31