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
4 changes: 2 additions & 2 deletions plugins/nodejs.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
38 changes: 35 additions & 3 deletions plugins/nodejs/setup-corepack.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions testscripts/plugin/nodejs_corepack_autodetect.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading