【问题标题】:Can Visual Studio Code be configured to launch with nodemon可以将 Visual Studio Code 配置为使用 nodemon 启动吗
【发布时间】:2016-03-30 17:51:40
【问题描述】:

我已将nodemon 作为全局包安装在我的系统中。 当我在 cmd 中执行 nodemon 时,它可以工作。

但是当我在这个launch.json文件中使用vscode时,vscode会抛出这个异常:

请求启动:运行时可执行文件 XXX\XXX\XXX\XXX\nodemon 不存在

launch.json 是:

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

当我删除 runtimeExecutable 中的 nodemin 时,它与 node 完美运行

【问题讨论】:

  • VS Code 需要“runtimeExecutable”的绝对路径。所以在 OS X 上使用“/usr/local/bin/nodemon”将使启动配置工作。但是,在调试会话结束时,VS Code 将杀死 nodemon,这并不是使用 nodemon 的初衷。这就是为什么下面的答案是更好的方法。

标签: node.js nodemon visual-studio-code


【解决方案1】:

首先,安装 nodemon 作为开发依赖:

npm install --save-dev nodemon

对于较新版本的 VS Code,像这样设置您的 .vscode/launch.json 文件:

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

最重要的部分是指向 nodemon 脚本的 runtimeExecutable 属性和指向入口点脚本的 program 属性。

如果您使用较旧的 VS Code(您不应该这样做),请尝试以下启动配置:

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

最重要的部分是指向 nodemon 脚本的 program 属性和指向正常入口点脚本的 args 属性。

【讨论】:

  • 知道如何将参数传递给nodemon,例如--watch /server/**.ts --exec ts-node server/server.ts?通常它只是 package.json 脚本或 cmd 行中的一大行。 nodemon --watch server/**/*.ts --ignore server/**/*.spec.ts --exec ts-node server/server.ts
  • 我自己没试过,但是你应该可以在“args”数组中传递你需要的所有参数。对于您提到的示例,请尝试使用"args": ["--watch", "/server/**.ts", "--ignore", "server/**/*.spec.ts", "--exec", "ts-node", "server/server.ts"]
  • 知道如何使用 babel-node/es6 做到这一点吗?这是我要运行的实际 nodemon 命令:nodemon src/shim-host/index.js --exec babel-node --babel-preset-es2015 但是当我将相关文件添加到 program 并将 nodemon 参数添加到 runtimeArgs 时它不起作用
  • @devdropper87:您需要更改“args”,而不是“runtimeArgs”。试试这个:"args": ["${workspaceRoot}/src/shim-host/index.js", "--exec", "babel-node", "--babel-preset-es2015"]
  • @AdrianT 我试过了,这是生成的 nodemon 命令(然后崩溃):[nodemon] starting babel-node ${workspaceRoot}/src/shim-host/index.js --babel-preset-es2015
【解决方案2】:

我无法使用附加的调试器获得@AdrianT 的答案。似乎有一种更新的内置支持方式可以做到这一点:

  1. 打开启动配置下拉菜单并选择“添加配置...”
  2. 选择“Node.js:Nodemon 设置”

它会在你的 launch.json 中添加类似这样的内容:

{
        "type": "node",
        "request": "launch",
        "name": "nodemon",
        "runtimeExecutable": "nodemon",
        "program": "${workspaceRoot}/app.js",
        "restart": true,
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
}

确保您的“程序”设置是正确的入口点脚本。

您需要全局安装 nodemon 才能使其正常工作 (npm install -g nodemon)(根据 the documentation)

您的应用现在运行,您可以设置将被命中的断点并将控制台记录到集成终端窗口。

请注意,终止调试会话只会终止要调试的程序,而不是 nodemon 本身。要终止 nodemon,请在集成终端中按 Control-C。

【讨论】:

  • 这应该是公认的答案,因为它是现在这样做的正确方法。
  • 如果你不喜欢运行全局nodemon,你也可以使用npm安装nodemon,然后设置"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
  • flags 呢,它们在 runtimeExecutable 中吗
【解决方案3】:

在 Visual Studio 代码中创建启动配置:

{
    "name": "Attach",
    "type": "node",
    "request": "attach",
    "port": 5858,
    "restart": true
}

从命令行运行 nodemon:nodemon --debug server.js

现在从 VC 和 vuala '附加'。

【讨论】:

  • 为什么是端口 5858?如果 nodemon 在 3000 上运行。尝试 3000 并且它没有做任何事情(没有错误没有)似乎也没有工作。 @Yevgeni
  • 所以我将--debug 标志传递给nodemon,我在Debugger listening on 127.0.0.1:5858 上看到远程调试,但奇​​怪的是VSCode 连接到某些东西,但似乎没有遇到断点
【解决方案4】:

附加绝对是一个简单的选择。为了确保您的代码中断,请确保您使用--inspect-brk(节点 8+)运行 nodemon,例如:

nodemon --inspect-brk src/app.js

启动 nodemon 后,将记录为调试连接打开的端口:

Debugger listening on ws://127.0.0.1:9229/someUUID

您可以使用该端口来构建您的启动配置,这非常简单:

{
  "type": "node",
  "request": "attach",
  "name": "Attach",
  "port": 9229,
  "restart": true
},

【讨论】:

    【解决方案5】:

    我尝试了 Adrian 和 Mathew 建议的解决方案。如果您在 macOS 上(也许也在 Linux 上),它们似乎工作得很好。在 Windows 上,也许马修的解决方案更稳定。将两者结合起来有一个可以同时兼容 macOS、Windows 和 Linux 的解决方案,并确保我们不会遇到诸如“找不到路径”之类的错误,我发现使用全局安装的 nodemon 进行 调试 似乎要稳定得多。

    • 全局安装nodemon(如果你之前没有安装过)npm i -g nodemon
    • 将以下内容添加到.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"
            }
          ]
        }
    

    当然,我们仍然可以在本地安装 nodemon 并在开发时运行它。

    【讨论】:

      【解决方案6】:

      不,目前不能。但我设法使用 nodemon 让这个有点工作。我从 Grunt 开始。但是一个等效的命令行应该做同样的事情。

      编辑:经过一个晚上的测试,我可以说下面的方法仍然有些不稳定:S,连接会间歇性地失败,有时断点会被忽略。

      EDIT2:您还可以在 Gruntfile 中使用 ['--debug-brk=5860'] 为 nodeArgs 指定非默认调试端口。我还被建议使用--debug-brk 而不是--debug。也许这将消除当前的脆弱性。如果有帮助,我会回来在这里提及(我目前已切换项目)。

      如果这可能有助于它在 Windows 10 上的当前 VS 代码版本(例如 v0.10.6)中使用以下设置的任何人。但它也可能在 Mac 上工作(我可能会稍后检查)。但请注意,有时我必须在调试器获取文件之前通过更改+保存文件来触发重建。

      /.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'
              }
          }
      }
      

      我猜调试端口 5858 是默认的,因为这里没有指定(注意它在上面的 launch.json 中。)

      【讨论】:

      【解决方案7】:

      https://github.com/Microsoft/vscode-recipes/tree/master/nodemon

      上面的链接帮助我成功调试了 nodemon + express 应用程序。这些步骤在那里得到了很好的解释。

      launch.json

      {
      "version": "0.2.0",
      "configurations": [
          {
              "type": "node",
              "request": "attach",
              "name": "Node: Nodemon",
              "processId": "${command:PickProcess}",
              "restart": true,
              "protocol": "inspector",
          }
      ]
      

      }

      npm 脚本

      "dev-server": "nodemon ***--inspect*** server.js"
      

      步骤:

      1. 使用 npm 脚本运行服务器。请注意 --inspect arg in 脚本
      2. 启动可视化代码调试器,将显示一个提示 选择节点服务器进程
      3. 选择节点服务器进程

      现在你应该可以调试了。

      如果对您没有帮助,请查看官方文档,其中解释了配置选项。 https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools

      【讨论】:

        【解决方案8】:

        什么对我有用没有全局安装和使用打字稿:

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

        为了不让ts-node出问题,在nodemon.json我加了:

        {
            "execMap": {
                "ts": "npx ts-node"
            }
        }
        

        【讨论】:

          【解决方案9】:

          是的,你可以!在最近的更新中,您可以将调试器附加到正在运行的 Nodemon 进程。 This page has more information。在页面上搜索 nodemon 以查看说明。

          【讨论】:

          【解决方案10】:

          我使用 Node Exec 插件。它允许您通过按 F8 和 F9(适用于编辑器中打开的文件)在 vcs 中运行和停止节点应用程序。作为(临时)解决方法,这可能会有所帮助。

          【讨论】:

            【解决方案11】:

            Nodemon 作为本地依赖项

            我也无法立即得到@Adrian T 的回答。但这只是一个小细节,必须对其进行更改才能使其正常工作。所以我们开始:

            1. 在顶级.vscode文件夹中创建一个launch.json文件
            2. 在 VS Code 中打开文件
            3. 使用内置按钮添加配置 - 将在编辑器中呈现 - 为 Node.js: Nodemon Setup 添加配置
            4. 在生成的配置中更改键runtimeExecutable:
            "program": "${workspaceFolder}/app.js",
            
            // diff from Adrian T
            -- "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
            ++ "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
            

            您现在正在使用本地依赖项中的可执行文件(请参阅SO thread)

            在answer 下方,Martin 也有一个答案(只是为了正确):

            如果您不喜欢运行全局 nodemon,您也可以使用 npm 安装 nodemon,然后设置“runtimeExecutable”:“${workspaceFolder}/node_modules/.bin/nodemon”,– Martin 2018 年 2 月 22 日在22:25

            Nodemon 作为全局依赖项

            "runtimeExecutable": "nodemon", 仅当 nodemon 的可执行文件本身是环境变量 PATH 的一部分(不是 `%appdata% 等)时才有效。由于大多数情况并非如此,因此您需要指定绝对路径(请参阅文档 here 和 here)。

            【讨论】:

              【解决方案12】:

              对于任何尝试在 Windows 上使用 express-generator 创建的 ExpressJS 项目设置 nodemon 的人,当 nodemon 安装为本地开发依赖项(例如 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"
                  }
              }
              

              【讨论】:

                【解决方案13】:

                它在主终端中对我来说很好用。 不要使用VS代码终端,使用主系统终端。

                【讨论】:

                • 一个好的答案将始终包括解释为什么这会解决问题,以便 OP 和任何未来的读者可以从中学习。
                猜你喜欢
                • 2015-07-12
                • 2022-11-26
                • 2021-11-08
                • 1970-01-01
                • 2022-11-02
                • 2021-06-16
                • 2023-01-17
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多