可以将Visual Studio代码配置为使用nodemon启动

问题描述 投票:33回答:8

我在我的系统中安装了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.js nodemon visual-studio-code
8个回答
39
投票

首先,将nodemon安装为dev依赖项:

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代码(您不应该使用),请尝试以下启动配置:

{
  "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属性。


27
投票

我无法得到@AdrianT的答案,附带调试器。看起来有一种更新的内置支持方式来执行此操作:

  1. 打开Launch Configuration下拉列表并选择“Add configuration ...”
  2. 选择“Node.js:Nodemon Setup”

它会在你的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。


15
投票

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

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

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

现在从VC和vuala'附加'。


3
投票

附加绝对是一个简单的选择。为了确保代码中断,请确保使用--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
},

1
投票

不,目前它不能。但是我设法使用nodemon来解决这个问题。我是从Grunt开始的。但是等效的命令行也应该这样做。

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

编辑2:您还可以使用['--debug-brk=5860']nodeArgs在Gruntfile中指定非默认调试端口。我也被建议使用--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中。)


0
投票

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


0
投票

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


0
投票

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

以上链接帮助我成功调试了nodemon + express app。这些步骤很好地解释了。

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脚本运行服务器。请注意 - 在脚本中检查arg
  2. 启动可视代码调试器,将显示一个提示,以选择节点服务器进程
  3. 选择节点服务器进程

现在你应该能够调试了。

如果它没有帮助你,那么请看看官方文档,配置选项在那里解释。 https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools

© www.soinside.com 2019 - 2024. All rights reserved.