openclaw/node-runtime-env.mjs
Peter Steinberger 070b7479dc
fix(update): hand the private Node runtime to npm lifecycle children (#164677)
When the updater or recovery selected the compatible private Node runtime and ran npm's JavaScript CLI with it, npm lifecycle children (preinstall and friends) resolved plain node from PATH and picked the unsupported system Node, so the install rejected itself. One shared PATH handoff now prepends the selected runtime's bin directory for every package-manager spawn in recovery, update environments, and explicit npm/pnpm execution, preserving npm_config_node and the explicit executable, scoped to the child only. Real-npm regression: lifecycle child reported v22.22.3 before, the private runtime after. Closes #164525.
2026-10-03 19:05:29 -07:00

19 lines
780 B
JavaScript

import path from "node:path";
/** Keep lifecycle children on the explicitly selected runtime without changing npm config. */
export function withNodeRuntimePath(env, nodePath, platform = process.platform) {
const paths = platform === "win32" ? path.win32 : path.posix;
const keys = Object.keys(env)
.sort()
.filter((key) => (platform === "win32" ? key.toUpperCase() === "PATH" : key === "PATH"));
const key = keys[0] ?? "PATH";
const directory = paths.dirname(nodePath);
const entries = (env[key] ? env[key].split(paths.delimiter) : []).filter(
(entry) => entry !== directory,
);
const result = { ...env };
for (const pathKey of keys.length ? keys : [key]) {
result[pathKey] = [directory, ...entries].join(paths.delimiter);
}
return result;
}