Can Visual Studio Code be configured to launch with nodemon

47,022

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
47,022

Related videos on Youtube

Mickel Sierra
Author by

Mickel Sierra

Updated on July 09, 2022

Comments

  • Mickel Sierra
    Mickel Sierra almost 2 years

    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

    • Andre Weinand
      Andre Weinand over 8 years
      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.
    • Rohith K P
      Rohith K P about 5 years
  • Andre Weinand
    Andre Weinand over 8 years
    I've created this VSCode feature request: github.com/Microsoft/vscode/issues/2103
  • Tomas Voracek
    Tomas Voracek over 7 years
    There is nothing on code.visualstudio.com/Docs/editor/debugging mentioning nodemon...
  • Mark Pieszak - Trilon.io
    Mark Pieszak - Trilon.io over 7 years
    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
    Mark Pieszak - Trilon.io over 7 years
    Why port 5858? If nodemon is running on 3000. Tried 3000 and it doesn't do anything (no error nothing) neither seem to work. @Yevgeni
  • Mark Pieszak - Trilon.io
    Mark Pieszak - Trilon.io over 7 years
    So I passed the --debug flag to nodemon and I see remote debuggin on Debugger listening on 127.0.0.1:5858 but strangely VSCode connects to something, but doesn't seem to hit breakpoints
  • Adrian Theodorescu
    Adrian Theodorescu over 7 years
    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"]
  • devdropper87
    devdropper87 almost 7 years
    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
  • Adrian Theodorescu
    Adrian Theodorescu almost 7 years
    @devdropper87: You need to change "args", not "runtimeArgs". Try this: "args": ["${workspaceRoot}/src/shim-host/index.js", "--exec", "babel-node", "--babel-preset-es2015"]
  • devdropper87
    devdropper87 almost 7 years
    @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
    devdropper87 almost 7 years
    Ah I see thanks, but then how would nodemon be leveraged in that case?
  • Adrian Theodorescu
    Adrian Theodorescu almost 7 years
    @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.
  • mjbates7
    mjbates7 almost 7 years
    Can anyone provide a working solution to this showing the args? I'm running into the same issue.
  • Aron
    Aron over 6 years
  • Rafael del Rio
    Rafael del Rio over 6 years
    This is should be the accepted answer as it's the correct way to do this now.
  • Martin
    Martin about 6 years
    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",
  • ABMagil
    ABMagil over 5 years
    This seems to not work- running VSCode 1.30.2. Matthew's answer worked for me
  • Adrian Theodorescu
    Adrian Theodorescu over 5 years
    @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.
  • philk
    philk over 4 years
    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.
  • Muhammad Umer
    Muhammad Umer over 4 years
    what about flags, do they go in runtimeExecutable
  • Tyler2P
    Tyler2P over 2 years
    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.
  • dotNET
    dotNET over 2 years
    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.