Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
A tiny cross-platform promise based wrapper around child_process.spawn.
The shell-exec npm package allows you to execute shell commands from within a Node.js application. It provides a simple interface to run shell commands and capture their output, making it useful for tasks such as automation, scripting, and system administration.
Execute Shell Commands
This feature allows you to execute shell commands and capture their output. In this example, the 'ls -la' command lists the contents of the current directory in long format, and the output is logged to the console.
const shell = require('shell-exec');
shell('ls -la').then(console.log).catch(console.error);
Handle Command Output
This feature allows you to handle the standard output (stdout) and standard error (stderr) of the executed command. In this example, the 'echo Hello, World!' command prints 'Hello, World!' to stdout, and the output is logged to the console.
const shell = require('shell-exec');
shell('echo Hello, World!').then(result => {
console.log('stdout:', result.stdout);
console.log('stderr:', result.stderr);
}).catch(console.error);
Error Handling
This feature allows you to handle errors that occur when executing a shell command. In this example, an invalid command is executed, and the error is caught and logged to the console.
const shell = require('shell-exec');
shell('invalidcommand').then(console.log).catch(error => {
console.error('Command failed:', error);
});
The child_process module is a built-in Node.js module that provides the ability to spawn child processes and execute shell commands. It offers more control and flexibility compared to shell-exec, but requires more boilerplate code to handle the execution and output of commands.
Execa is a modern alternative to the child_process module. It provides a simpler and more powerful interface for executing shell commands, with better support for promises and improved error handling. Execa is more feature-rich and flexible compared to shell-exec.
ShellJS is a portable Unix shell commands for Node.js. It provides a comprehensive set of shell commands that can be used within a Node.js application. ShellJS is more feature-rich and offers a wider range of shell commands compared to shell-exec.
A tiny cross-platform promise based wrapper around child_process.spawn.
Install the package locally within you project folder with your package manager:
With npm
:
npm install shell-exec
With yarn
:
yarn add shell-exec
With pnpm
:
pnpm add shell-exec
import shellExec from 'shell-exec'
shellExec('echo Hi!').then(console.log).catch(console.log)
shellExec(command, options)
Parameters:
command
{String | Array} - String or Array of commands to runoptions
{Object} - Options object passed to child_process.spawn
The function then returns a Promise
.
See the hosted docs here.
Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.
FAQs
A tiny cross-platform promise based wrapper around child_process.spawn.
The npm package shell-exec receives a total of 206,945 weekly downloads. As such, shell-exec popularity was classified as popular.
We found that shell-exec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.