The most powerful aspect of Node. js is "cross-platform ". You only need to pay attention to the encoding, and your code will go through Windows, Linux, and OSX platforms. The following article describes how to use Node. js to compile a cross-platform spawn statement. The most powerful aspect of Node. js is "cross-platform ". You only need to pay attention to the encoding, and your code will go through Windows, Linux, and OSX platforms. The following article describes how to use Node. js to compile a cross-platform spawn statement.
Preface
Node. js is cross-platform, that is, it can run on Windows, OSX, and Linux platforms. Many Node. js developers develop code on OSX and then deploy the code on the Linux server. Because OSX and Linux are both Unix-based, they have many commonalities. Windows is also officially supported by Node. js. As long as you write code in the correct way, you can run it without any pressure on each platform.
The Node. js subprocess module has a spawn function that can be used to call system commands, such as Linux and macOS.
const spawn = require('child_process').spawn;spawn('npm', { stdio: 'inherit'});
To call the npm command.
However, if the same statement is executed on Windows, an error is reported.
Error: spawn npm ENOENT at exports._errnoException (util.js:855:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32) at onErrorNT (internal/child_process.js:344:16) at nextTickCallbackWith2Args (node.js:455:9) at process._tickCallback (node.js:369:17) at Function.Module.runMain (module.js:432:11) at startup (node.js:141:18) at node.js:980:3
Because in Windows, when we execute npm, we actually execute npm. cmd batch processing, while on Windows ,. cmd ,. bat batch processing cannot run independently from the interpreter cmd.exe.
Therefore, we need to explicitly call cmd
spawn('cmd', ['/c', 'npm'], { stdio: 'inherit'});
Alternatively, when calling the spawn function, set the shell option to true to call cmd implicitly (this option is added from Node. js v6)
spawn('npm', { stdio: 'inherit', shell: true});
In addition, although you do not need to set the shell option on Linux, macOS, and other systems, the command can be executed normally. Setting the shell to true will not impede command execution, it will only generate an unnecessary shell Process and affect the performance.
Therefore, if you want to write cross-platform spawn commands without additional overhead, you can write
Const process = require ('process'); const {spawn} = require ('child _ Process'); spawn ('npm ', {stdio: 'herit ', // use shell: process only when the current running environment is Windows. platform = 'win32 '});
Third-party Module cross-spawn
For the cross-platform Writing of spawn functions, in addition to processing code by yourself, there are also third-party modules that encapsulate relevant details, such as cross-spawn.
This module automatically determines whether to generate a shell to execute the given command based on the current running platform when calling the spawn function.
In addition
Supports Node. js versions earlier than v6 (at least Node. js v6 is required to use shell options );
Cross-platform support for shebang;
It is more convenient to escape characters in commands and parameters.
Install
npm install cross-spawn
Usage
const spawn = require('cross-spawn');spawn('npm', { stdio: 'inherit'});
For more details about how to use Node. js to compile cross-platform spawn statements, refer to the PHP Chinese website!