{"record":{"id":"d2b498eb873c7a0e","repo":"Unitech/pm2","slug":"cannot-find-package-json","errorCode":null,"errorMessage":"Cannot find package.json","messagePattern":"Cannot find package\\.json","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"modules/pm2-io-bpm/configuration.js","lineNumber":74,"sourceCode":"\n  static init (conf, doNotTellPm2) {\n    const packageFilepath = Configuration.findPackageJson()\n    let packageJson\n\n    if (!conf.module_conf) {\n      conf.module_conf = {}\n    }\n    conf.apm = {\n      type: 'node',\n      version: VERSION\n    }\n\n    if (conf.isModule === true) {\n      /**\n       * Merge package.json metadata\n       */\n      try {\n        if (!packageFilepath) throw new Error('Cannot find package.json')\n        packageJson = require(packageFilepath)\n\n        conf.module_version = packageJson.version\n        conf.module_name = packageJson.name\n        conf.description = packageJson.description\n\n        if (packageJson.config) {\n          conf = Object.assign(conf, packageJson.config)\n          conf.module_conf = packageJson.config\n        }\n      } catch (e) {\n        throw new Error(e)\n      }\n    } else {\n      conf.module_name = process.env.name || 'outside-pm2'\n      try {\n        if (!packageFilepath) throw new Error('Cannot find package.json')\n        packageJson = require(packageFilepath)","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/Unitech/pm2/blob/31adee8048dbbc1ee36a7df7cdbcaca2559708df/modules/pm2-io-bpm/configuration.js#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"Standalone scripts started without a package.json; webpack/esbuild single-file bundles; running the agent from a tmp dir.","solutions":["Ensure a package.json exists in or above the directory of your app's main entry.","Run your app as a real project (npm init) rather than a loose script.","If the code is not actually a PM2 module, leave conf.isModule unset/false so the missing file is only debug-logged."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst path = require('path');\nfunction findPkg(startDir) {\n  let dir = path.resolve(startDir);\n  for (let i = 0; i < 5; i++) {\n    const p = path.join(dir, 'package.json');\n    if (fs.existsSync(p)) return p;\n    dir = path.dirname(dir);\n  }\n  return null;\n}\nif (conf.isModule === true && !findPkg(__dirname)) {\n  throw new Error('No package.json found; cannot initialize as a module');\n}","typeGuard":null,"tryCatchPattern":"try {\n  apm.init(conf);\n} catch (e) {\n  if (/Cannot find package\\.json/.test(e.message)) { conf.isModule = false; apm.init(conf); }\n  else throw e;\n}","preventionTips":["Run modules from a real project tree containing a package.json.","If your code is not a PM2 module, do not set conf.isModule = true.","For bundled single-file builds, generate a minimal package.json next to the entry."],"tags":["bpm","package-json","module"],"backgroundTag":null,"analyzedSha":"31adee8048dbbc1ee36a7df7cdbcaca2559708df","analyzedAt":"2026-08-13T03:49:51.765Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}