Detailed explanation of writing cross platform spawn statement with Node. js

  • 2021-07-18 06:59:48
  • OfStack

Preface

Node. js is cross-platform, meaning it runs on Windows, OSX, and Linux platforms. Many Node. js developers develop on OSX and then deploy the code to the Linux server. Since both OSX and Linux are based on Unix, they have a lot in common. Windows is also the official platform supported by Node. js. As long as you write the code in the right way, you can run without pressure on each platform.

The child processes of Node. js ( child_process ) There are 1 under the module spawn Function, can be used to call commands on the system, such as Linux, macOS and other systems, we can execute


const spawn = require('child_process').spawn;

spawn('npm', {
 stdio: 'inherit'
});

To call npm Orders.

However, if the same statement is executed on Windows, an error will be 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 on Windows, when we execute npm, what we actually execute is npm.cmd Batch processing, and on Windows, .cmd , .bat Batch processing cannot be detached cmd.exe This 1 interpreter runs independently.

Therefore, we need to explicitly call cmd


spawn('cmd', ['/c', 'npm'], {
 stdio: 'inherit'
});

Or use it when calling the spawn Function, set the shell The option is true To implicitly call the spawn 0 (This option was added from Node. js v 6 version)


spawn('npm', {
 stdio: 'inherit',
 shell: true
});

In addition, although it is not required to set on Linux, macOS and other systems, shell Option, the command can also be executed normally; Settings shell For true It will not hinder the execution of the command, but will generate an extra shell process that is unnecessary, which will affect the performance.

Therefore, if you want to write a cross-platform spawn Command without adding extra overhead, you can write it like this


const process = require('process');
const { spawn } = require('child_process');

spawn('npm', {
 stdio: 'inherit',
 //  Only in the current running environment is  Windows  Use the  shell
 shell: process.platform === 'win32'
});

Third party module cross-spawn

About spawn Function cross-platform writing, in addition to their own code to do processing, there are also third-party modules encapsulated the relevant details, such as cross-spawn.

Using this module, you can call the spawn Function, automatically decide whether to generate 1 according to the current running platform shell To execute the given command.

Moreover, it can also

Support Node. js versions below v6 (using shell Option requires at least Node. js v6); Support shebang; across platforms; It is more convenient to escape characters in commands and parameters.

Installation


npm install cross-spawn

Usage


const spawn = require('cross-spawn');

spawn('npm', {
 stdio: 'inherit'
});

Reference document

Deriving. bat and. cmd files on Windows

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: