
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
spawn-sync
Advanced tools
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.
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());
The child_process module is a core Node.js module that provides the ability to spawn child processes. It includes both asynchronous and synchronous methods (spawn, exec, execFile, fork, and their synchronous counterparts). Unlike spawn-sync, child_process is built into Node.js and does not require an additional package installation.
Execa is a modern alternative to the child_process module. It provides a more user-friendly API for executing shell commands and scripts. Execa supports both asynchronous and synchronous execution, similar to spawn-sync, but offers additional features like improved error handling and promise support.
ShellJS is a portable (Windows/Linux/macOS) implementation of Unix shell commands on Node.js. It provides a synchronous API for executing shell commands, similar to spawn-sync. ShellJS is designed to be more cross-platform and user-friendly, making it a good alternative for scripting tasks.
This used to be a polyfill for require('child_process').spawnSync
but now all actively maintained node versions already support spawnSync
, so this is just a stub that re-exports spawnSync
.
You should remove this library from your dependencies and just do:
var spawnSync = require('child_process').spawnSync;
var result = spawnSync('node',
['filename.js'],
{input: 'write this to stdin'});
if (result.status !== 0) {
process.stderr.write(result.stderr);
process.exit(result.status);
} else {
process.stdout.write(result.stdout);
process.stderr.write(result.stderr);
}
MIT
FAQs
Exports child_process.spawnSync
The npm package spawn-sync receives a total of 1,299,364 weekly downloads. As such, spawn-sync popularity was classified as popular.
We found that spawn-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.