【问题标题】:VSCode editor - Restart NodeJs server when file is changedVSCode 编辑器 - 文件更改时重新启动 NodeJs 服务器
【发布时间】:2016-05-16 20:07:24
【问题描述】:

我使用 Visual Studio Code 作为 NodeJS 项目的编辑器。

目前我在项目中更改文件时需要手动重启服务器。

VSCode中是否有任何插件或配置更改可以在我更改文件时自动重启NodeJS服务器。

【问题讨论】:

  • 使用nodemon 而不是node 运行您的脚本。
  • nodemonnode-dev 都不适用于 VSCode。一旦它终止进程,调试器就会认为它已经终止并关闭。
  • 好像有功能请求:github.com/Microsoft/vscode/issues/2103
  • 谢谢,我订阅了该问题以获取通知。
  • 问题 github.com/Microsoft/vscode/issues/2103 已经关闭,但还是不行。

标签: node.js visual-studio-code


【解决方案1】:

您现在可以在 VS Code 中使用 Nodemon 来实现此目的。我今天测试了 Nodemon 对 VS Code 的支持,它对我来说效果很好。以下是我的 VS Code 详细信息。

  • 版本:1.9.1
  • 提交:f9d0c687ff2ea7aabd85fb9a43129117c0ecf519
  • 日期:2017-02-09T00:26:45.394Z
  • 外壳:1.4.6
  • 渲染器:53.0.2785.143
  • 节点:6.5.0

我在全局安装了Nodemon npm install -g nodemon 并创建了 VS Code 启动配置,如下所示

    {
    "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"
   }

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

【讨论】:

  • 请注意 --debug 已被弃用,根据我的经验可能会导致应用崩溃,请改用 --inspect
  • 我可以配置它来刷新浏览器中的页面吗?
  • 以下是我的launch.json的内容。这为我解决了这个问题。 { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/server.js", "runtimeExecutable": "nodemon", "restart": true, "cwd": "${workspaceRoot}", "runtimeArgs": ["--inspect=5858"], "port": 5858, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" } ] }
【解决方案2】:

你也可以在本地安装nodemon npm install nodemon --save-dev

以及下面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"
  }
]

【讨论】:

  • 当nodemon在文件更改后重新启动时,它会抛出Starting inspector on 127.0.0.1:5858 failed: address already in use的错误
  • 它应该和 webstruck 的方法一样工作,除了你需要在你的项目中本地安装 nodemon
【解决方案3】:

编辑我们的应用程序文件后自动重新启动调试器:

在 Vscode 午餐程序中为 nodejs 添加调试器配置,如下图所示。

在下面的文件路径中添加两行:

.vscode/launch.json

"runtimeExecutable": "nodemon",
"restart":true

假设你已经全局安装了 nodemon

npm install nodemon -g 

更多信息请关注官方文档链接:https://code.visualstudio.com/docs/nodejs/nodejs-debugging

【讨论】:

    【解决方案4】:

    使用pm2 观察您的代码并自动重启

    npm install pm2 -g
    npm install pm2
    

    process.json

    {
        name        : "App",
        script      : "app.js",
        watch       : true,
    }
    

    你可以找到演示@ https://github.com/sivasankars/jade-title-rendering

    【讨论】:

      【解决方案5】:

      添加到 Siva 的 cmets

      这将使用新的 pm2 版本转到生态系统.config.js

      module.exports = {

       apps : [{
          **name: 'App',
          script: 'app.js',
          watch: false,**
          max_memory_restart: '1G',
          env: {
            NODE_ENV: 'development'
          },
          env_production: {
            NODE_ENV: 'production'
          }
        }],
      
        deploy : {
          production : {
            user : 'node',
            host : '212.83.163.1',
            ref  : 'origin/master',
            repo : 'git@github.com:repo.git',
            path : '/var/www/production',
            'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
          }
        }
      };
      

      【讨论】:

        【解决方案6】:

        以下是我运行 Express 服务器的方法:

        {
              "name": "Nodemon Launch Server",
              "type": "node",
              "request": "launch",
              "cwd": "${workspaceFolder}",
              "runtimeExecutable": "nodemon",
              "runtimeArgs": [
                  "--inspect-brk"
              ],
              "program": "${workspaceFolder}/bin/www",
              "restart": true,
              // "console": "integratedTerminal",
              "internalConsoleOptions": "neverOpen"
        }
        

        【讨论】:

          【解决方案7】:

          对我来说,Oleksandr 的回答不起作用。

          代替

          "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
          

          以下应该可以工作。

          "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
          

          还要注意,确保节点程序文件应该在当前目录或项目目录中,例如,nodemon-ning ./../file.js 可能不起作用,但 nodemon 在文件更改时不会重新启动程序,具体取决于在 nodemon 版本或环境上。您的脚本文件应位于例如./file.js

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-26
            • 2014-03-22
            • 2019-02-13
            • 2019-05-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-21
            相关资源
            最近更新 更多