Home > Blockchain >  Node ExpressJS debugging on VS Code is not watching for changes
Node ExpressJS debugging on VS Code is not watching for changes

Time:10-24

I have a Node ExpressJS backend test server which I run (and watch for changes) with the following command:

$ npm run dev

That's because I have the following: package.json:

{
  ...
  "main": "index.js",
  "type": "module",
  "scripts": {
    ...
    "dev": "cross-env DEBUG=app nodemon --exec babel-node src/index.js"
  },
  ...
}

I also can debug the backend code with VS Code by using this: /.vscode/launch.json:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Backend Debug",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/backend/src/index.js",
      "runtimeExecutable": "${workspaceFolder}/backend/node_modules/.bin/babel-node",
      "runtimeArgs": ["--nolazy"]
    }
  ]
}

My problem is: Even though I can properly debug the backend code with the launch.json above, the process is not watching for changes. I mean, if I do a change on the code then the server doesn't restart with new changes.

Any idea on how to do this?

Thanks!

CodePudding user response:

Your script in the package.json file uses the popular nodemon package to track changes to your code and auto-restart the app accordingly: "cross-env DEBUG=app nodemon --exec babel-node src/index.js".

However, your VS Code run configuration starts your app without using nodemon. It just runs your node program directly.

I'm not familiar enough with VS Code to know exactly what your run configuration should be in order to properly use nodemon and VS Code, but some other people have described a way to do that here.

  • Related