Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode editor - Restart NodeJs server when file is changed

I am using Visual Studio Code as my editor for NodeJS project.

Currently I need to manually restart server when I change files in my project.

Is there any plugin or configuration change in VSCode that can restart NodeJS server automatically when I make change in files.

like image 796
DamirM Avatar asked Feb 06 '16 23:02

DamirM


2 Answers

You can now use Nodemon with VS Code to achieve this. I tested the Nodemon support for VS Code today and it worked well for me. Below are my VS Code details.

  • Version: 1.9.1
  • Commit: f9d0c687ff2ea7aabd85fb9a43129117c0ecf519
  • Date: 2017-02-09T00:26:45.394Z
  • Shell: 1.4.6
  • Renderer: 53.0.2785.143
  • Node: 6.5.0

I installed Nodemon globally npm install -g nodemon and created VS Code launch configuration as below

    {
    "name": "Nodemon Launch Server",
    "type": "node",
    "request": "launch",
    "cwd": "${workspaceRoot}",
    "runtimeExecutable": "nodemon",
    "runtimeArgs": [
        "--debug=5858"
    ],
    "program": "${workspaceRoot}/server.js",
    "restart": true,
    "port": 5858,
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen"
   }

Reference: https://code.visualstudio.com/docs/editor/node-debugging#_restarting-debug-sessions-automatically-when-source-is-edited

like image 197
webstruck Avatar answered Sep 28 '22 09:09

webstruck


You can also install nodemon locally npm install nodemon --save-dev.

And the following example of configurations of VS Code launch.json:

[
  {
    "name": "Nodemon",
    "type": "node",
    "request": "launch",
    "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
    "program": "${workspaceFolder}/src/server/index.js",
    "restart": true,
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen"
  }
]
like image 37
Oleksandr Afanasiev Avatar answered Sep 28 '22 09:09

Oleksandr Afanasiev