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
- 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.
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
- 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.
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.