Promise-based wrappers for Node.js core APIs that modernize callback-based methods to work with async/await patterns
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Execute shell commands and spawn processes with promise support. Provides modern async/await compatibility for Node.js child_process operations.
Execute shell commands with promise support.
/**
* Execute a shell command in a new process
* @param command - Command string to execute
* @param options - Execution options
* @returns Promise resolving to stdout string
*/
function exec(command, options): Promise<string>;
/**
* Execute a file directly without shell interpretation
* @param file - Path to executable file
* @param args - Array of arguments to pass to executable
* @param options - Execution options
* @returns Promise resolving to stdout string
*/
function execFile(file, args, options): Promise<string>;Usage Examples:
const { exec, execFile } = require('mz/child_process');
// Execute shell commands
async function runCommands() {
try {
// Get Node.js version
const nodeVersion = await exec('node --version');
console.log('Node version:', nodeVersion.trim());
// List files in current directory
const files = await exec('ls -la');
console.log('Directory contents:', files);
// Execute with options
const result = await exec('echo "Hello World"', {
cwd: '/tmp',
env: { ...process.env, CUSTOM_VAR: 'value' }
});
console.log('Command output:', result);
} catch (error) {
console.error('Command failed:', error.message);
// Error object contains additional properties:
// - error.code: exit code
// - error.signal: signal that terminated the process
// - error.stdout: stdout output
// - error.stderr: stderr output
}
}
// Execute files directly
async function runExecutable() {
try {
// Execute Node.js directly with arguments
const output = await execFile('node', ['--version']);
console.log('Node version:', output.trim());
// Execute with options
const result = await execFile('/bin/echo', ['Hello', 'World'], {
cwd: '/tmp'
});
console.log('Echo output:', result);
} catch (error) {
console.error('Execution failed:', error);
}
}
// Callback support is still available
exec('pwd', (err, stdout, stderr) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Current directory:', stdout.trim());
}
});Both exec and execFile will reject with an error object when:
The error object contains additional properties:
code: Process exit code (number)signal: Signal that terminated the process (string)stdout: Standard output (string)stderr: Standard error output (string)const { exec } = require('mz/child_process');
async function handleCommandErrors() {
try {
await exec('nonexistent-command');
} catch (error) {
console.log('Exit code:', error.code);
console.log('Signal:', error.signal);
console.log('Stdout:', error.stdout);
console.log('Stderr:', error.stderr);
}
}Both functions accept an options object with the following properties:
interface ExecOptions {
/** Current working directory for the command */
cwd?: string;
/** Environment variables */
env?: object;
/** Character encoding for stdout/stderr */
encoding?: string;
/** Shell to execute the command with (exec only) */
shell?: string;
/** Timeout in milliseconds */
timeout?: number;
/** Largest amount of data in bytes allowed on stdout/stderr */
maxBuffer?: number;
/** Kill signal to use when timeout is reached */
killSignal?: string;
/** User ID to run the command as */
uid?: number;
/** Group ID to run the command as */
gid?: number;
/** Whether to run in detached mode */
detached?: boolean;
/** Whether to create new console window (Windows only) */
windowsVerbatimArguments?: boolean;
}thenify-all to wrap native child_process methodsexec uses shell interpretation, execFile executes directly for better security