简体   繁体   中英

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

Running commands via Node's execFile on windows is a nightmare. Many a time, when I get a "Command failed" error if I copy the "failed" command from the error it executes just fine. Sometimes, trying windowsVerbatimArguments: true helps but usually, it does not.

For example, right now I am running Visual Studio's MSBuild.exe, with the following parameters in Node.js - I have printed the exact array passed to 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 Studio\2017\Community\MSBuild\15.0\Bin\amd64\MSBuild.exe

If I copy these items from the array and paste them into the command line it will run just fine. Clearly, Node.js is messing up when passing the command. Because this has happened to me many times, just a few weeks ago with Irfan View, I am not looking for a solution for this specific problem - I would just be asking a similar question again very soon.

I am looking for a guide how to see what is Node.js actually passing so that I can make a guess how to edit the parameters so that they are accepted by the command I call.

How to see what is Node.js exactly sunning why I call execFile ?

You can use NODE_DEBUG environment variable to get extra info out of NodeJS.

You can find docs here .

For example this 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');
    }
});

And to enable debug logging for child_process module execute:

NODE_DEBUG=CHILD_PROCESS node index.js

(On Windows the environment variable seems to be set differently)

set NODE_DEBUG=CHILD_PROCESS & node index.js

in my case it produces this kind of output:

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

PS

Setting NODE_DEBUG=* will produce all debug log information that is available in NodeJS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM