diff --git a/plugins/nodejs.json b/plugins/nodejs.json index bc398057744..9153dd4f186 100644 --- a/plugins/nodejs.json +++ b/plugins/nodejs.json @@ -1,8 +1,8 @@ { "$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.", + "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: some Nodejs packages do not bundle Corepack, including `nodejs-slim` and newer Nodejs versions (25+). With those packages you must add the `corepack` package to your devbox.json separately (for example, run `devbox add corepack`) for Corepack to be available.", "env": { "DEVBOX_COREPACK_BIN_DIR": "{{ .Virtenv }}/corepack-bin", "PATH": "{{ .Virtenv }}/corepack-bin:$PATH" diff --git a/plugins/nodejs/setup-corepack.mjs b/plugins/nodejs/setup-corepack.mjs index 1ca1dd175cc..4e4d2796c12 100644 --- a/plugins/nodejs/setup-corepack.mjs +++ b/plugins/nodejs/setup-corepack.mjs @@ -28,10 +28,42 @@ if (!corepackBinDir) { } // Enable Corepack, installing the pnpm/yarn/npm shims into corepackBinDir. -run("corepack", ["enable", "--install-directory", corepackBinDir]); +// Only attempt package-manager activation if enabling succeeded; if it failed +// for any reason (Corepack missing, offline, etc.) there's nothing to activate. +if (enableCorepack()) { + // Activate the package manager pinned in package.json's "packageManager" + // field. + activatePinnedPackageManager(); +} + +// enableCorepack runs `corepack enable`. It returns true on success. If the +// `corepack` binary itself is missing it prints an actionable warning and +// returns false, because some Node packages (such as nodejs-slim, and Node.js +// 25+) no longer bundle Corepack (see issue #2791). Other failures (e.g. being +// offline) are ignored so they don't block shell initialization. +function enableCorepack() { + try { + execFileSync("corepack", ["enable", "--install-directory", corepackBinDir], { + stdio: "inherit", + }); + return true; + } catch (err) { + if (err && err.code === "ENOENT") { + warnCorepackUnavailable(); + } + return false; + } +} -// Activate the package manager pinned in package.json's "packageManager" field. -activatePinnedPackageManager(); +function warnCorepackUnavailable() { + process.stderr.write( + "[devbox] Warning: DEVBOX_COREPACK_ENABLED is set but the `corepack` " + + "command was not found. Some Node packages (such as nodejs-slim, and " + + "Node.js 25+) no longer bundle Corepack. Add the `corepack` package to " + + "your devbox.json (for example, run `devbox add corepack`) to use " + + "Yarn or pnpm.\n", + ); +} function activatePinnedPackageManager() { if (process.env.DEVBOX_DISABLE_NODEJS_PACKAGE_MANAGER_AUTODETECT) { diff --git a/testscripts/plugin/nodejs_corepack_autodetect.test.txt b/testscripts/plugin/nodejs_corepack_autodetect.test.txt index 6b75c9b7f5f..67ba216ad66 100644 --- a/testscripts/plugin/nodejs_corepack_autodetect.test.txt +++ b/testscripts/plugin/nodejs_corepack_autodetect.test.txt @@ -30,6 +30,16 @@ cp package-esm.json package.json exec devbox run -- node -e 'console.log("case3-ok")' stdout 'case3-ok' +# Case 4: a Node package that does not bundle Corepack, e.g. nodejs-slim +# (issue #2791). Shell init must still succeed, and the plugin must print an +# actionable warning instead of failing later with a cryptic +# "corepack: command not found". +exec devbox rm nodejs +exec devbox add nodejs-slim +exec devbox run -- node -e 'console.log("case4-ok")' +stdout 'case4-ok' +stderr 'Warning: DEVBOX_COREPACK_ENABLED is set but the .corepack. command was not found' + -- package-no-pkgmgr.json -- { "name": "nodejs-corepack-autodetect",