diff --git a/plugins/nodejs.json b/plugins/nodejs.json index bc398057744..ea18dc9c2c2 100644 --- a/plugins/nodejs.json +++ b/plugins/nodejs.json @@ -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": { diff --git a/plugins/nodejs/setup-corepack.mjs b/plugins/nodejs/setup-corepack.mjs index 1ca1dd175cc..e9a4f077084 100644 --- a/plugins/nodejs/setup-corepack.mjs +++ b/plugins/nodejs/setup-corepack.mjs @@ -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(); @@ -65,7 +80,8 @@ 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" }); @@ -73,3 +89,16 @@ function run(command, args) { // 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.", + ); +}