Unitech/pm2 · error · Error

Cannot find package.json

Error message

Cannot find package.json

What it means

In pm2-io-bpm's Configuration.init, when conf.isModule === true the agent walks up from require.main.filename to find a package.json (findPackageJson). If none is found within two parent directories it returns null, and the module branch throws because module metadata (name/version/description) is mandatory for a module.

Source

Thrown at modules/pm2-io-bpm/configuration.js:74

  static init (conf, doNotTellPm2) {
    const packageFilepath = Configuration.findPackageJson()
    let packageJson

    if (!conf.module_conf) {
      conf.module_conf = {}
    }
    conf.apm = {
      type: 'node',
      version: VERSION
    }

    if (conf.isModule === true) {
      /**
       * Merge package.json metadata
       */
      try {
        if (!packageFilepath) throw new Error('Cannot find package.json')
        packageJson = require(packageFilepath)

        conf.module_version = packageJson.version
        conf.module_name = packageJson.name
        conf.description = packageJson.description

        if (packageJson.config) {
          conf = Object.assign(conf, packageJson.config)
          conf.module_conf = packageJson.config
        }
      } catch (e) {
        throw new Error(e)
      }
    } else {
      conf.module_name = process.env.name || 'outside-pm2'
      try {
        if (!packageFilepath) throw new Error('Cannot find package.json')
        packageJson = require(packageFilepath)

View on GitHub (pinned to 31adee8048)

Solutions

  1. Ensure a package.json exists in or above the directory of your app's main entry.
  2. Run your app as a real project (npm init) rather than a loose script.
  3. If the code is not actually a PM2 module, leave conf.isModule unset/false so the missing file is only debug-logged.
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
const path = require('path');
function findPkg(startDir) {
  let dir = path.resolve(startDir);
  for (let i = 0; i < 5; i++) {
    const p = path.join(dir, 'package.json');
    if (fs.existsSync(p)) return p;
    dir = path.dirname(dir);
  }
  return null;
}
if (conf.isModule === true && !findPkg(__dirname)) {
  throw new Error('No package.json found; cannot initialize as a module');
}

Try / catch

try {
  apm.init(conf);
} catch (e) {
  if (/Cannot find package\.json/.test(e.message)) { conf.isModule = false; apm.init(conf); }
  else throw e;
}

Prevention

When it happens

Trigger: Loading the BPM/APM agent with isModule:true from a location with no package.json in the require resolution chain — a standalone script, an eval'd module, or a bundled single-file build where require.main.filename doesn't map to the source tree.

Common situations: Standalone scripts started without a package.json; webpack/esbuild single-file bundles; running the agent from a tmp dir.

Related errors


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