Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@npmcli/promise-spawn

Package Overview
Dependencies
Maintainers
4
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@npmcli/promise-spawn

spawn processes the way the npm cli likes to do

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.7M
increased by6.26%
Maintainers
4
Weekly downloads
 
Created

What is @npmcli/promise-spawn?

The @npmcli/promise-spawn package is designed to execute shell commands or scripts with a promise-based interface. It simplifies working with child processes in Node.js by providing a straightforward way to spawn processes and handle their output, errors, and exit codes asynchronously. This package is particularly useful for building Node.js applications that need to interact with the system's shell or execute external commands as part of their operation.

What are @npmcli/promise-spawn's main functionalities?

Executing a simple command

This feature allows you to execute a simple command (in this case, 'echo') and print its output, error output, and exit code. The 'stdioString' option is used to treat the output and error as strings.

const promiseSpawn = require('@npmcli/promise-spawn');

async function runCommand() {
  const { stdout, stderr, code } = await promiseSpawn('echo', ['Hello, world!'], { stdioString: true });
  console.log(`Output: ${stdout}`);
  console.error(`Error: ${stderr}`);
  console.log(`Exit code: ${code}`);
}

runCommand();

Executing a command with error handling

This feature demonstrates executing a command that is expected to fail (attempting to list a nonexistent directory) and handling the error gracefully. The catch block captures the error, allowing the application to respond appropriately.

const promiseSpawn = require('@npmcli/promise-spawn');

async function runCommandWithErrorHandling() {
  try {
    const { stdout } = await promiseSpawn('ls', ['-l', '/nonexistent'], { stdioString: true });
    console.log(`Output: ${stdout}`);
  } catch (err) {
    console.error(`Error: ${err.message}`);
  }
}

runCommandWithErrorHandling();

Other packages similar to @npmcli/promise-spawn

FAQs

Package last updated on 26 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc