如何使用 vscode 中的自定义参数启动 nodejs

How to start nodejs with custom params from vscode

有什么方法可以用额外的命令行参数启动nodeJS吗?

赞:

--harmony_generators
--harmony_arrow_functions

更新:

目前的解决方法:

  1. 创建 .bat (windows) 文件:

    • {{节点路径}}\node.exe --harmony_generators --harmony_arrow_functions %*
  2. 将路径添加到您的 .bat 文件作为 runtimeExecutable 的来源 .\settings\launch.json

  3. 利润:)

编辑./settings/launch.json(调试菜单 > 齿轮图标)

有一个 args 您可以编辑的条目

在 VSCode 的预览版中,尚无法将参数从 launch.json 传递到节点。但是上面提到的解决方法工作正常。 我已经在我们这边创建了一个错误,并确保在下一个版本中修复它。

安德烈·韦南德, Visual Studio代码


更新:

自 v0.3 起在 VSCode 中进行修复,在 .settings/launch.json 中进行此修复:

"configurations": [
    {
        ...

        // Optional arguments passed to the runtime executable.
        "runtimeArgs": [],

        ...

例如运行 Node.js (v0.12) 支持 ES6 使用 "runtimeArgs": ["--harmony"],

在我的例子中,我是 运行 这个命令和参数: 节点 app.js 读取 --title="SomeTitle"

为了解决这个问题,我使用了这个:

"args": [
            "read",
            "\--\--title\=='SomeTitle'"
        ]

输出是这样的:

node --inspect=10398 --debug-brk app.js 读取 --title='Title'

这很适合我。

使用 runtimeArgs 的建议对我不起作用,因为它通过 "before" 调用我的 app.js。

使用当前版本 1.36.1,您可以将参数添加到 launch.json 示例:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/index.js",
            "args":["my-url=http://192.168.1.24:8984/api/", "port=3000"]
        }
    ]
}

在您的节点应用程序中,您可以捕获参数:

 process.argv.forEach(function (val, index, array) 
 {
   console.log(val);
 }  

现在您可以运行您的Visual Studio代码调试并查看参数如何显示

如果您 运行 从控制台 应用程序 它应该是这样的:

node index.js my-url=http://192.168.1.24:8984/api/ port=3000

两种情况下的输出都是:

my-url=http://192.168.1.24:8984/api/
port=3000