Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/nodejs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/main/.schema/devbox-plugin.schema.json",
"version": "0.0.4",
"version": "0.0.5",
"name": "nodejs",
"readme": "Devbox automatically configures Corepack for Nodejs when DEVBOX_COREPACK_ENABLED=1. You can install Yarn or Pnpm by adding them to your `package.json` file using `packageManager`\nCorepack binaries will be installed in your local `.devbox` directory\n\nWhen Corepack is enabled, Devbox also activates the package manager pinned in your `package.json` `packageManager` field automatically. Set DEVBOX_DISABLE_NODEJS_PACKAGE_MANAGER_AUTODETECT=1 to disable this behavior.\n\nNote: newer versions of Nodejs (25+) no longer bundle Corepack, so you must add the `corepack` package to your devbox.json separately for Corepack to be available.",
"env": {
Expand Down
33 changes: 31 additions & 2 deletions plugins/nodejs/setup-corepack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ if (!corepackBinDir) {
}

// Enable Corepack, installing the pnpm/yarn/npm shims into corepackBinDir.
run("corepack", ["enable", "--install-directory", corepackBinDir]);
try {
execFileSync("corepack", ["enable", "--install-directory", corepackBinDir], {
stdio: "inherit",
});
} catch (err) {
if (err && err.code === "ENOENT") {
// Corepack isn't bundled with this Node.js package (e.g. nodejs-slim, or
// Node.js 25+, see issue #2791). Without a message the user is later left
// with a cryptic "command not found" for yarn/pnpm and no idea why, so warn
// with actionable guidance and stop: there is nothing to activate.
warnCorepackMissing();
process.exit(0);
}
// Any other failure (e.g. a non-zero exit while offline) is non-fatal: fall
// through and still attempt activation, as the plugin did before.
}

// Activate the package manager pinned in package.json's "packageManager" field.
activatePinnedPackageManager();
Expand Down Expand Up @@ -65,11 +80,25 @@ function activatePinnedPackageManager() {
}

// Run a command, inheriting stdio so Corepack's output is visible. Failures
// must not block shell initialization.
// must not block shell initialization, so errors are reported but never
// rethrown.
function run(command, args) {
try {
execFileSync(command, args, { stdio: "inherit" });
} catch {
// Ignore: e.g. Corepack unavailable, or offline during activation.
}
}

// Print actionable guidance when the `corepack` binary is missing, so the user
// understands why yarn/pnpm aren't available and how to fix it.
function warnCorepackMissing() {
console.error(
"[devbox] nodejs plugin: `corepack` was not found, so Corepack-managed " +
"package managers (such as yarn and pnpm) will be unavailable.",
);
console.error(
"[devbox] Your Node.js package does not bundle Corepack (e.g. nodejs-slim, " +
"or Node.js 25+). Add it explicitly with `devbox add corepack` to enable it.",
);
}
Loading