Use Node. js to compile cross-platform spawn statements.

Source: Internet
Author: User

Use Node. js to compile cross-platform spawn statements.

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.

Node. js sub-processes (child_process)spawn Functions can be used to call system commands, such as Linux and macOS.

const spawn = require('child_process').spawn;spawn('npm', { stdio: 'inherit'});

To callnpm 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 run npm, what we actually execute isnpm.cmd Batch Processing, while on Windows,.cmd,.bat Batch Processing cannot be separatedcmd.exe This interpreter runs independently.

Therefore, we need to explicitly call cmd

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

Or usespawn Setshell Option:true To callcmd (This option is added from Node. js v6)

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

In addition, you do not need to setshell Option, the command can also be executed normally; Setshell Istrue It will not impede command execution, but will generate an additional unnecessary shell process, affecting the performance.

Therefore, if you want to write cross-platformspawn Command, but do not want to add 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

Aboutspawn Function cross-platform writing, in addition to processing code by yourself, there are also third-party modules that encapsulate relevant details, such as cross-spawn.

This module can be calledspawn The function automatically determines whether to generateshell To execute the given command.

In addition

  • Node. js versions earlier than v6 are supported (Useshell Option requires at least Node. js v6 );
  • 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'});

References

Export the. bat and. cmd files on Windows

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.