🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

spawn-sync

Package Overview
Dependencies
Maintainers
4
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spawn-sync

Exports child_process.spawnSync

2.0.0
latest
Source
npm
Version published
Weekly downloads
1.4M
-17.18%
Maintainers
4
Weekly downloads
 
Created

What is spawn-sync?

The spawn-sync npm package provides synchronous versions of Node.js's child_process.spawn and child_process.spawnSync functions. It allows you to execute shell commands and scripts synchronously, which can be useful in scenarios where you need to ensure that a command has completed before proceeding with the next step in your code.

What are spawn-sync's main functionalities?

Synchronous Command Execution

This feature allows you to execute shell commands synchronously. In this example, the 'ls -lh /usr' command is executed, and the output is printed to the console.

const spawnSync = require('spawn-sync');
const result = spawnSync('ls', ['-lh', '/usr']);
console.log(result.stdout.toString());

Handling Command Output

This feature allows you to handle the output of the executed command. In this example, the 'node -v' command is executed to get the Node.js version, and the output is printed to the console. If there's an error, it is logged to the console.

const spawnSync = require('spawn-sync');
const result = spawnSync('node', ['-v']);
if (result.error) {
  console.error('Error:', result.error);
} else {
  console.log('Node version:', result.stdout.toString());
}

Passing Environment Variables

This feature allows you to pass custom environment variables to the executed command. In this example, the 'printenv' command is executed with a custom environment variable 'CUSTOM_VAR' set to 'HelloWorld'. The output is printed to the console.

const spawnSync = require('spawn-sync');
const result = spawnSync('printenv', [], { env: { ...process.env, CUSTOM_VAR: 'HelloWorld' } });
console.log(result.stdout.toString());

Other packages similar to spawn-sync

FAQs

Package last updated on 14 May 2018

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