我如何才能看到 Node.js 作为命令行参数传递的到底是什么?

How can I see what EXACTLY is Node.js passing as command line arguments?

运行 在 windows 上通过 Node 的 execFile 命令是一场噩梦。很多时候,当我收到“命令失败”错误时,如果我从错误中复制“失败”命令,它执行得很好。有时,尝试 windowsVerbatimArguments: true 会有帮助,但通常不会。

例如,现在我是 运行宁 Visual Studio 的 MSBuild.exe,Node.js 中有以下参数 - 我已经打印了传递给的确切数组execFile:

Running MSBuild with parameters:  [
  'D:\PROJECT\vcxproj\HelperProject.vcxproj',
  '-t:OutputBuildMacro',
  '-p:ProjectToImport=D:\PROJECT\vcxproj\HelperProject.vcxproj;PropertyToGet="OutputType,BlueProjectsVersionPropsGlobalIncluded"'
]
Executable path:  C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild.0\Bin\amd64\MSBuild.exe

如果我从数组中复制这些项目并将它们粘贴到命令行中,它将 运行 正常。显然,Node.js 在传递命令时搞砸了。因为这在我身上发生过很多次,就在几周前 Irfan View,我不是在寻找这个特定问题的解决方案 - 我很快就会再次问类似的问题。

我正在寻找如何查看Node.js 实际 传递的指南,以便我可以猜测如何编辑参数以便接受它们通过我调用的命令。

怎么看Node.js到底是晒太阳为什么我叫execFile?

您可以使用 NODE_DEBUG 环境变量从 NodeJS 中获取额外信息。

您可以找到文档 here

例如这个index.js:

const { execFile } = require('child_process');

execFile('/bin/ls', ['/Volumes'], {}, (err, stdout, stderr) => {
    if (err) {
        console.error('Command failed');
    } else {
        console.log('Command succeded');
    }
});

并为 child_process 模块启用调试日志记录:

NODE_DEBUG=CHILD_PROCESS node index.js

(在 Windows 上,环境变量似乎设置不同)

set NODE_DEBUG=CHILD_PROCESS & node index.js

在我的例子中,它产生了这种输出:

CHILD_PROCESS 85208: spawn {
  cwd: null,
  env: null,
  gid: undefined,
  uid: undefined,
  shell: false,
  windowsHide: false,
  windowsVerbatimArguments: false,
  args: [ '/bin/ls', '/Volumes' ],
  detached: false,
  envPairs: [
    'COMMAND_MODE=unix2003',
    ...
    'TERM=xterm-256color',
    'NODE_DEBUG=CHILD_PROCESS',
  ],
  file: '/bin/ls'
}

P.S.

设置NODE_DEBUG=* 将生成 NodeJS 中可用的所有调试日志信息。