nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.48k stars 284 forks source link

child_process.spawnSync() not return anything. #728

Closed netcore-jroger closed 7 years ago

netcore-jroger commented 7 years ago

In my company's computer:

let result = childProcess.spawnSync('D:/Program Files/nodejs/node.exe', ['--version']);

work very well, can get return value. but

let result = childProcess.spawnSync('node', ['--version']);

not work and the code behind it will not execute.

I use myself computer:

let result = childProcess.spawnSync('node', ['--version']);

was work very well.

I have added D:\Program Files\nodejs\ to the environment variable.

This problem caused my vscode editor to not work properly. see#45048199

vsemozhetbyt commented 7 years ago

Can you provide the output of the childProcess.spawnSync('node', ['--version']); where this does not work?

netcore-jroger commented 7 years ago

@vsemozhetbyt

childProcess.spawnSync('node', ['--version']);

not return anything. I just found that the shell: true can run properly.

    let result = childProcess.spawnSync('node', ['--version'], {
        shell: true
    });

    console.log(result);

work very well. but shell default value is false. Can I change the default value in global?

gibfahn commented 7 years ago

@netcore-jroger I suggest you use execSync, which actually spawns a shell to run the command in.

gibfahn commented 7 years ago

~But spawnSync returns things for me:~ I'm not on Windows, sorry.

Works for me: ![image](https://user-images.githubusercontent.com/15943089/28153326-7c5a588c-67ad-11e7-9960-491d58e0263c.png) ![image](https://user-images.githubusercontent.com/15943089/28153371-babf47a4-67ad-11e7-92e4-ccb0bf9ad7d6.png) ``` ➜ ~ ❯ node ~ > require('child_process').spawnSync('node', ['--version']); { status: 0, signal: null, output: [ null, , ], pid: 10931, stdout: , stderr: } ```
netcore-jroger commented 7 years ago

1

netcore-jroger commented 7 years ago
C:\Users\JRoger> node
> require('child_process').spawnSync('node', ['--version'], {shell: true});
Output: ```console { status: 0, signal: null, output: [ null, , ], pid: 38208, stdout: , stderr: , envPairs: [ 'ALLUSERSPROFILE=C:\\ProgramData', 'APPDATA=C:\\Users\\JRoger\\AppData\\Roaming', 'ChocolateyInstall=C:\\ProgramData\\chocolatey', 'ChocolateyLastPathUpdate=周四 5月 18 17:32:23 2017', 'CLASSPATH=.;C:\\Program Files\\Java\\jdk1.8.0_131\\lib\\dt.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\lib\\tools.j ar;', 'CommonProgramFiles=C:\\Program Files\\Common Files', 'CommonProgramFiles(x86)=C:\\Program Files (x86)\\Common Files', 'CommonProgramW6432=C:\\Program Files\\Common Files', 'COMPUTERNAME=JROGER-PC', 'ComSpec=C:\\Windows\\system32\\cmd.exe', 'FSHARPINSTALLDIR=C:\\Program Files (x86)\\Microsoft SDKs\\F#\\4.1\\Framework\\v4.0\\', 'HOME=C:\\Users\\JRoger', 'HOMEDRIVE=C:', 'HOMEPATH=\\Users\\JRoger', 'JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131', 'LOCALAPPDATA=C:\\Users\\JRoger\\AppData\\Local', 'LOGONSERVER=\\\\JROGER-PC', 'NUMBER_OF_PROCESSORS=8', 'OneDrive=D:\\Users\\JRoger\\OneDrive', 'OS=Windows_NT', 'Path=C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Win dows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Microsoft SQL Server\\Client SDK\\ODBC\\130\\Tools\\B inn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\1 30\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\ManagementStudio\\;C:\\Program Files\\M icrosoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files\\M icrosoft\\Web Platform Installer\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files (x8 6)\\Microsoft SQL Server\\120\\DTS\\Binn\\;D:\\Program Files\\Python27;C:\\Program Files\\dotnet\\;D:\\Program Files\\ap ache-maven-3.5.0\\bin;"C:\\Program Files\\Java\\jdk1.8.0_131\\bin;";"C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\bin;";C :\\ProgramData\\chocolatey\\bin;D:\\Program Files (x86)\\LINQPad5;C:\\Program Files (x86)\\Microsoft Visual Studio\\2017 \\Enterprise\\MSBuild\\15.0\\Bin\\amd64;C:\\Users\\JRoger\\AppData\\Roaming\\npm;D:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\Microsoft VS Code\\bin;D:\\Program Files\\nodejs;D:\\Program Files\\nodejs\\;C:\\Users\\JRoger\\AppData\\L ocal\\Microsoft\\WindowsApps;D:\\Program Files\\Python27;D:\\Program Files\\apache-maven-3.5.0\\bin;"C:\\Program Files\\ Java\\jdk1.8.0_131\\bin;";"C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\bin;";C:\\Program Files (x86)\\Microsoft Visual S tudio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\amd64;D:\\Program Files (x86)\\Microsoft VS Code\\bin;D:\\Program Files\\no dejs;C:\\Users\\JRoger\\AppData\\Roaming\\npm', 'PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL', 'PROCESSOR_ARCHITECTURE=AMD64', 'PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel', 'PROCESSOR_LEVEL=6', 'PROCESSOR_REVISION=5e03', 'ProgramData=C:\\ProgramData', 'ProgramFiles=C:\\Program Files', 'ProgramFiles(x86)=C:\\Program Files (x86)', 'ProgramW6432=C:\\Program Files', 'PSModulePath=D:\\Users\\JRoger\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Module s;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Pow erShell\\Modules\\;C:\\Program Files\\WindowsPowerShell\\Modules\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\Power Shell\\ResourceManager\\AzureResourceManager\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\PowerShell\\ServiceManage ment\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\PowerShell\\Storage\\', 'PUBLIC=C:\\Users\\Public', 'SESSIONNAME=Console', 'SystemDrive=C:', 'SystemRoot=C:\\Windows', 'TEMP=C:\\Users\\JRoger\\AppData\\Local\\Temp', 'TMP=C:\\Users\\JRoger\\AppData\\Local\\Temp', 'USERDOMAIN=JROGER-PC', 'USERDOMAIN_ROAMINGPROFILE=JROGER-PC', 'USERNAME=JRoger', 'USERPROFILE=C:\\Users\\JRoger', 'VBOX_MSI_INSTALL_PATH=D:\\Program Files\\Oracle\\VirtualBox\\', 'VS140COMNTOOLS=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\', 'VSSDK140Install=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VSSDK\\', 'windir=C:\\Windows' ], options: { shell: true, windowsVerbatimArguments: true, file: 'C:\\Windows\\system32\\cmd.exe', args: [ 'C:\\Windows\\system32\\cmd.exe', '/s', '/c', '"node --version"' ], envPairs: [ 'ALLUSERSPROFILE=C:\\ProgramData', 'APPDATA=C:\\Users\\JRoger\\AppData\\Roaming', 'ChocolateyInstall=C:\\ProgramData\\chocolatey', 'ChocolateyLastPathUpdate=周四 5月 18 17:32:23 2017', 'CLASSPATH=.;C:\\Program Files\\Java\\jdk1.8.0_131\\lib\\dt.jar;C:\\Program Files\\Java\\jdk1.8.0_131\\lib\\tool s.jar;', 'CommonProgramFiles=C:\\Program Files\\Common Files', 'CommonProgramFiles(x86)=C:\\Program Files (x86)\\Common Files', 'CommonProgramW6432=C:\\Program Files\\Common Files', 'COMPUTERNAME=JROGER-PC', 'ComSpec=C:\\Windows\\system32\\cmd.exe', 'FSHARPINSTALLDIR=C:\\Program Files (x86)\\Microsoft SDKs\\F#\\4.1\\Framework\\v4.0\\', 'HOME=C:\\Users\\JRoger', 'HOMEDRIVE=C:', 'HOMEPATH=\\Users\\JRoger', 'JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131', 'LOCALAPPDATA=C:\\Users\\JRoger\\AppData\\Local', 'LOGONSERVER=\\\\JROGER-PC', 'NUMBER_OF_PROCESSORS=8', 'OneDrive=D:\\Users\\JRoger\\OneDrive', 'OS=Windows_NT', 'Path=C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\ Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Microsoft SQL Server\\Client SDK\\ODBC\\130\\Tools \\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server \\130\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\Binn\\ManagementStudio\\;C:\\Program Files \\Microsoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files \\Microsoft\\Web Platform Installer\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\120\\DTS\\Binn\\;D:\\Program Files\\Python27;C:\\Program Files\\dotnet\\;D:\\Program Files\ \apache-maven-3.5.0\\bin;"C:\\Program Files\\Java\\jdk1.8.0_131\\bin;";"C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\bin; ";C:\\ProgramData\\chocolatey\\bin;D:\\Program Files (x86)\\LINQPad5;C:\\Program Files (x86)\\Microsoft Visual Studio\\2 017\\Enterprise\\MSBuild\\15.0\\Bin\\amd64;C:\\Users\\JRoger\\AppData\\Roaming\\npm;D:\\Program Files\\Git\\cmd;C:\\Prog ram Files (x86)\\Microsoft VS Code\\bin;D:\\Program Files\\nodejs;D:\\Program Files\\nodejs\\;C:\\Users\\JRoger\\AppData \\Local\\Microsoft\\WindowsApps;D:\\Program Files\\Python27;D:\\Program Files\\apache-maven-3.5.0\\bin;"C:\\Program File s\\Java\\jdk1.8.0_131\\bin;";"C:\\Program Files\\Java\\jdk1.8.0_131\\jre\\bin;";C:\\Program Files (x86)\\Microsoft Visua l Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\amd64;D:\\Program Files (x86)\\Microsoft VS Code\\bin;D:\\Program Files\ \nodejs;C:\\Users\\JRoger\\AppData\\Roaming\\npm', 'PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL', 'PROCESSOR_ARCHITECTURE=AMD64', 'PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel', 'PROCESSOR_LEVEL=6', 'PROCESSOR_REVISION=5e03', 'ProgramData=C:\\ProgramData', 'ProgramFiles=C:\\Program Files', 'ProgramFiles(x86)=C:\\Program Files (x86)', 'ProgramW6432=C:\\Program Files', 'PSModulePath=D:\\Users\\JRoger\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Mod ules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules;C:\\Program Files (x86)\\Microsoft SQL Server\\130\\Tools\\ PowerShell\\Modules\\;C:\\Program Files\\WindowsPowerShell\\Modules\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\Po werShell\\ResourceManager\\AzureResourceManager\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\PowerShell\\ServiceMan agement\\;C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\PowerShell\\Storage\\', 'PUBLIC=C:\\Users\\Public', 'SESSIONNAME=Console', 'SystemDrive=C:', 'SystemRoot=C:\\Windows', 'TEMP=C:\\Users\\JRoger\\AppData\\Local\\Temp', 'TMP=C:\\Users\\JRoger\\AppData\\Local\\Temp', 'USERDOMAIN=JROGER-PC', 'USERDOMAIN_ROAMINGPROFILE=JROGER-PC', 'USERNAME=JRoger', 'USERPROFILE=C:\\Users\\JRoger', 'VBOX_MSI_INSTALL_PATH=D:\\Program Files\\Oracle\\VirtualBox\\', 'VS140COMNTOOLS=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\', 'VSSDK140Install=C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VSSDK\\', 'windir=C:\\Windows' ], stdio: [ [Object], [Object], [Object] ] }, args: [ 'C:\\Windows\\system32\\cmd.exe', '/s', '/c', '"node --version"' ], file: 'C:\\Windows\\system32\\cmd.exe' } ```

_(edited by @vsemozhetbyt: added backticks and folded output)_
vsemozhetbyt commented 7 years ago

@netcore-jroger So if you run require('child_process').spawnSync('node', ['--version']); in the v6.11.1 RERL, it just exits without any output?

netcore-jroger commented 7 years ago

yes !

vsemozhetbyt commented 7 years ago

cc @nodejs/platform-windows

netcore-jroger commented 7 years ago

in terminal also without any output.

netcore-jroger commented 7 years ago

@vsemozhetbyt

require('child_process').execSync('node', ['--version']);

work very well.

netcore-jroger commented 7 years ago

I found a similar question link

gibfahn commented 7 years ago

In that case it's probably something to do with needing to spawn a batch shell on Windows. If you're running batch commands execSync is the better choice.

netcore-jroger commented 7 years ago

https://stackoverflow.com/questions/45047450/in-the-child-process-module-in-node-js

netcore-jroger commented 7 years ago

In fact, this is a problem with vscode editing. I have a little bit of debugging vscode code found this problem.

netcore-jroger commented 7 years ago

A lot of places in vscode use the child_process.spawn () function.

vsemozhetbyt commented 7 years ago

Do you mean node --version in the cmd.exe terminal also outputs nothing? What does where node output?

netcore-jroger commented 7 years ago

is there any way to update the shell in the global default value is true?

netcore-jroger commented 7 years ago

2

netcore-jroger commented 7 years ago

I manually modify all vscode editor code using the child_process.spawn () shell is true. The Vscode editor is working properly.

bzoz commented 7 years ago

@netcore-jroger, does this work for you, from a command shell: node -e "console.log(require('child_process').spawnSync('node', ['--version']))"

netcore-jroger commented 7 years ago

@bzoz not work. But the following code works fine:

node -e "console.log(require('child_process').spawnSync('node', ['--version'], {shell: true}))"
bzoz commented 7 years ago

Is there anything printed out? Any error message?

netcore-jroger commented 7 years ago

nothing.

bzoz commented 7 years ago

@netcore-jroger that is strange. You should get at least some error message.

Could you try couple more things? What does running where node returns? Does running require('child_process').spawnSync('node', ['--version']) from the REPL exits node? I mean, what you get when you do this: image

Also, is your D: drive unusual? A subst drive, ram drive, network share, etc>?

Can you also try running require('child_process').spawnSync('not existing') ?

Thanks for your answers!

netcore-jroger commented 7 years ago

@bzoz Look at the picture below: 3

vsemozhetbyt commented 7 years ago

@netcore-jroger In the last case you should escape backslashes: require('child_process').spawnSync('D:\\Program Files\\nodejs\\node.exe');.

netcore-jroger commented 7 years ago

Oh sorry. 4

The first parameter, if provided with a complete path, has always been normal.

vsemozhetbyt commented 7 years ago

Repeating @bzoz's question:

Also, is your D: drive unusual? A subst drive, ram drive, network share, etc>?

netcore-jroger commented 7 years ago

@vsemozhetbyt normal drive. 5

netcore-jroger commented 7 years ago

6

In good condition.

vsemozhetbyt commented 7 years ago

Does this happen only in REPL? Is there any output if you run a node file.js with this code in file.js:

console.log(require('child_process').spawnSync('node', ['--version']));
netcore-jroger commented 7 years ago

@vsemozhetbyt The same result is in the REPL and in the code file. When I specify the shell option is true, it is completely normal

vsemozhetbyt commented 7 years ago

Does this happen with any other .exe files from path env in C: and D: drives? Is require('child_process').spawnSync('notepad'); OK?

netcore-jroger commented 7 years ago

@vsemozhetbyt invoke-notepad

invoke notepad is ok.

vsemozhetbyt commented 7 years ago

Is require('child_process').spawnSync('python', ['-V']) OK (python is on the D: drive, right)?

netcore-jroger commented 7 years ago

can also be. 7

netcore-jroger commented 7 years ago

python install path: D:\Program Files\Python27\

vsemozhetbyt commented 7 years ago

Strange. cc @bnoordhuis, @cjihrig concerning child_process.

netcore-jroger commented 7 years ago

8 9

refack commented 7 years ago

What's strange in this picture is that the REPL seems to exit image

vsemozhetbyt commented 7 years ago

And node file.js also exits without any output or error.

netcore-jroger commented 7 years ago

@refack yes. exited.

refack commented 7 years ago

So I want to say it's something is the system outside of node. @netcore-jroger could you take a look at the event log (run eventvwr.msc) if it says anything about node.exe crashing.

netcore-jroger commented 7 years ago

I found into the installation directory and execute the command require ('child_process'). SpawnSync ('node', ['--version']); can work properly. But it will not work properly on the terminal.

14 10 11 12 13

netcore-jroger commented 7 years ago

@refack has nothing information in Windows Application events.

refack commented 7 years ago

@netcore-jroger Can you also post the output of set or cmd /k set

netcore-jroger commented 7 years ago

@refack set command. 15

refack commented 7 years ago

So there are two thing that are not common:

  1. Your %ProgramFiles% == C:\Program Files, but nodejs is in D:\Program Files\nodejs
  2. There are directories with " in your %Path%which is unusual, maybe even the cause... image

For example my %Path% is:

d:\code\node-cur$ set Path
Path=D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\VC\Tools\MSVC\14.10.25017\bin\HostX64\x64;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.2;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\MSBuild\15.0\bin\Roslyn;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\;C:\Program Files (x86)\Microsoft SDKs\F#\4.1\Framework\v4.0\;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x64;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\\MSBuild\15.0\bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Common7\IDE\;D:\bin\dev\VS\Microsoft Visual Studio 2017 Community\Common7\Tools\;C:\Tools\maint\conemu\ConEmu\Scripts;C:\Tools\maint\conemu;C:\Tools\maint\conemu\ConEmu;c:\Program Files\Microsoft MPI\Bin\;C:\Program Files\Docker\Docker\Resources\bin;C:\WINDOWS\system32\Tools;C:\WINDOWS\system32\downlevel;C:\WINDOWS\system32\Java\jre\bin;C:\WINDOWS\system32;C:\WINDOWS\system32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\Wbem;C:\WINDOWS;C:\bin\dev\node;C:\bin\dev\python27;C:\bin\dev\python27\Scripts;C:\bin\git\usr\bin;C:\bin\git\cmd;C:\Tools\maint;C:\Tools\dev;C:\code\tools;C:\code\tools\scripts;.\node_modules\.bin\;C:\Program Files\dotnet\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
PATHEXT=.JS;.;.CMD;.BAT;.EXE;.MSC;.COM

None of the entries has "

netcore-jroger commented 7 years ago

Oh sorry. my set command run in powershell. when I run in cmd.exe output result like this below picture: 1