hcengineering/platform · error · Error

"npm ${command}" encountered an error

Error message

"npm ${command}" encountered an error

What it means

_installPackage spawns `npm install` (or similar command) synchronously in the package install folder; a non-zero exit status means npm itself failed. The library throws the plain message '"npm <command>" encountered an error' because npm's own output (inherited via stdio: 'inherit') carries the real diagnostics. Note the outer catch then rewraps this as error 45, so this message usually appears nested inside 'Unable to install package: ...'.

Source

Thrown at foundations/core/common/scripts/install-run.js:644

        throw new Error(`Unable to create package.json: ${e}`);
    }
}
/**
 * Run "npm install" in the package install folder.
 */
function _installPackage(logger, packageInstallFolder, name, version, command) {
    try {
        logger.info(`Installing ${name}...`);
        const npmPath = getNpmPath();
        const platformNpmPath = _getPlatformPath(npmPath);
        const result = child_process__WEBPACK_IMPORTED_MODULE_0__.spawnSync(platformNpmPath, [command], {
            stdio: 'inherit',
            cwd: packageInstallFolder,
            env: process.env,
            shell: _isWindows()
        });
        if (result.status !== 0) {
            throw new Error(`"npm ${command}" encountered an error`);
        }
        logger.info(`Successfully installed ${name}@${version}`);
    }
    catch (e) {
        throw new Error(`Unable to install package: ${e}`);
    }
}
/**
 * Get the ".bin" path for the package.
 */
function _getBinPath(packageInstallFolder, binName) {
    const binFolderPath = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME, '.bin');
    const resolvedBinName = _isWindows() ? `${binName}.cmd` : binName;
    return path__WEBPACK_IMPORTED_MODULE_3__.resolve(binFolderPath, resolvedBinName);
}
/**
 * Returns a cross-platform path - windows must enclose any path containing spaces within double quotes.
 */

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Read the npm output printed above the error (stdio is inherited) for the root cause.
  2. Verify `npm --version` works and meets the script's expectations; update npm or Node if too old.
  3. Check registry connectivity/proxy settings (`npm config get proxy`, `npm config get registry`).
  4. Delete common/temp/install-run-* to force a clean reinstall, then rerun.

Example fix

// before (failing npm)
npm install --frozen-lockfile  # old npm, unknown flag
// after
npm install -g npm@latest && npm install --frozen-lockfile
Defensive patterns

Strategy: try-catch

Validate before calling

const { execSync } = require('child_process');
function assertNpmReady() {
  const v = execSync('npm --version').toString().trim(); // throws if npm missing/broken
  if (parseInt(v.split('.')[0], 10) < 7) throw new Error(`npm too old: ${v}`);
}
assertNpmReady();

Try / catch

try {
  installAndRun(logger, name, version, bin, args);
} catch (e) {
  if (/npm .* encountered an error|Unable to install package/.test(e.message)) {
    // npm's real diagnostics were printed above via stdio: 'inherit'
    console.error('npm install failed; see npm output above. Check registry access, lockfile, and npm version.');
  }
  throw e;
}

Prevention

When it happens

Trigger: The spawned npm process exits non-zero: unresolvable dependency tree, registry/network errors, invalid package.json in the install folder, npm not found (ENOENT on spawn), or unsupported npm flags for the installed npm version.

Common situations: Corporate proxy blocking the registry; incompatible lockfile (lockfileVersion mismatch with system npm); peer-dependency conflicts; disk space exhaustion; running an old npm that doesn't support flags like --frozen-lockfile.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/a282f0f88e935d35. Report an issue: GitHub.