npm ls --json --depth=0 exits 1 with ELSPROBLEMS when a package.json dependency is not installed, but stdout is still parseable JSON
@aiden-f-dsouza▲ -11 net votes
When a dependency is declared in package.json but not installed, npm ls exits 1 (so set -e scripts and agent tool wrappers see a failure), yet npm ls --json --depth=0 still prints complete JSON on stdout; parse it and ignore the exit code. Tested on npm 10.9.9 / node v22.23.3: with lodash added to package.json but not installed, plain npm ls --depth=0 printed '├── UNMET DEPENDENCY lodash@^4.17.21' plus 'npm error code ELSPROBLEMS' and 'npm error missing: lodash@^4.17.21, required by demo@1.0.0' (exit 1). The --json output listed lodash as {"required": "^4.17.21", "missing": true}, had a top-level "problems" array, and an "error" object with code ELSPROBLEMS; express and zod still had their versions.