Unitech/pm2 · error · Error

You cannot set a pm2_home and independent instance in same t

Error message

You cannot set a pm2_home and independent instance in same time

What it means

Thrown in the PM2 API constructor. The options object lets you control where PM2 stores its state (PM2_HOME): either pin it to an explicit path via pm2_home, or ask PM2 to mint a fresh unique home under /tmp via independent:true. These two are mutually exclusive — you cannot both name the home and ask for a random one in the same call.

Source

Thrown at lib/API.js:78

    this.daemon_mode = typeof(opts.daemon_mode) == 'undefined' ? true : opts.daemon_mode;
    this.pm2_home = conf.PM2_ROOT_PATH;
    this.public_key = conf.PUBLIC_KEY || opts.public_key || null;
    this.secret_key = conf.SECRET_KEY || opts.secret_key || null;
    this.machine_name = conf.MACHINE_NAME || opts.machine_name || null

    /**
     * CWD resolution
     */
    this.cwd = process.cwd();
    if (opts.cwd) {
      this.cwd = path.resolve(opts.cwd);
    }

    /**
     * PM2 HOME resolution
     */
    if (opts.pm2_home && opts.independent == true)
      throw new Error('You cannot set a pm2_home and independent instance in same time');

    if (opts.pm2_home) {
      // Override default conf file
      this.pm2_home = opts.pm2_home;
      conf = Object.assign(conf, path_structure(this.pm2_home));
    }
    else if (opts.independent == true && conf.IS_WINDOWS === false) {
      // Create an unique pm2 instance
      const crypto = require('crypto');
      var random_file = crypto.randomBytes(8).toString('hex');
      this.pm2_home = path.join('/tmp', random_file);

      // If we dont explicitly tell to have a daemon
      // It will go as in proc
      if (typeof(opts.daemon_mode) == 'undefined')
        this.daemon_mode = false;
      conf = Object.assign(conf, path_structure(this.pm2_home));
    }

View on GitHub (pinned to 31adee8048)

Solutions

  1. Remove one of the two keys — choose a single PM2 home strategy.
  2. If you want a dedicated isolated instance with a random home, drop pm2_home and keep independent:true.
  3. If you want a fixed, known location, drop independent and keep pm2_home.

Example fix

// before
new PM2({ pm2_home: '/var/pm2', independent: true });
// after
new PM2({ pm2_home: '/var/pm2' });
Defensive patterns

Strategy: validation

Validate before calling

function createPm2(opts) {
  if (opts && opts.pm2_home && opts.independent === true) {
    throw new TypeError('pm2_home and independent are mutually exclusive; pass exactly one');
  }
  return new (require('pm2'))(opts);
}

Type guard

function isValidPm2Opts(opts) {
  return !(opts && opts.pm2_home && opts.independent === true);
}

Prevention

When it happens

Trigger: Instantiating `new PM2({ pm2_home: '/some/path', independent: true })` (or pm2.connect with the same opts) — both keys present at once.

Common situations: Merging two config presets where one sets pm2_home and another sets independent:true; copy-pasting options from different examples; test harnesses that force independent mode combined with a fixed home.

Related errors


AI-assisted analysis of Unitech/pm2@31adee8048 (2026-08-13). Data as JSON: /api/errors/37b7c556dae94cc5. Report an issue: GitHub.