Flagged Respawn

2.0.0 · active · verified Sun Apr 19

flagged-respawn is a utility designed to address the common problem in Node.js applications where V8 or Node.js runtime flags (e.g., `--harmony`, `--experimental-modules`) are incorrectly placed within `process.argv`, leading them to be interpreted as application arguments rather than runtime options. This library identifies such flags and automatically respawns the Node.js process with the flags correctly prepended to the `node` executable command. The current stable version is 2.0.0. While there isn't a strict release cadence, it's part of the Gulp.js ecosystem, suggesting active maintenance for relevant Node.js versions. Its key differentiators include automatically handling stdio piping, ensuring the child process's exit code and signals are propagated to the parent, and providing a mechanism to prevent multiple respawns.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use flagged-respawn to correctly handle Node.js/V8 flags by respawning the process if necessary, ensuring flags are applied to the Node.js executable.

import flaggedRespawn from 'flagged-respawn';
import { fetch as fetchV8Flags } from 'v8flags';

// In a real application, you'd fetch v8flags once at startup.
// For simplicity, we'll fetch it synchronously here.
const v8flags = fetchV8Flags();

flaggedRespawn(v8flags, process.argv, function (ready, child, argv) {
  if (ready) {
    console.log('Application ready to run! Current process PID:', process.pid);
    console.log('Arguments:', argv.slice(2).join(' '));
    // Your main CLI application logic goes here.
    // Example: process a command or run a task.
    if (argv.includes('--test-mode')) {
      console.log('Running in test mode!');
    }
    // Simulate work
    setTimeout(() => {
      console.log('Application finished.');
      // process.exit(0); // Uncomment in a real app to exit cleanly
    }, 1000);
  } else {
    console.log('Special flags found, respawning process...');
    console.log('Old PID:', process.pid, 'New PID (if respawned):', child ? child.pid : 'N/A');
  }
});

view raw JSON →