diff --git a/plugins/nodejs.json b/plugins/nodejs.json index 3dc93c129e4..29a23152d1c 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.3", + "version": "0.0.4", "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.", "env": { @@ -9,11 +9,11 @@ }, "shell": { "init_hook": [ - "node \"{{ .Virtenv }}/bin/setup-corepack.js\"" + "node \"{{ .Virtenv }}/bin/setup-corepack.cjs\"" ] }, "create_files": { "{{ .Virtenv }}/corepack-bin": "", - "{{ .Virtenv }}/bin/setup-corepack.js": "nodejs/setup-corepack.js" + "{{ .Virtenv }}/bin/setup-corepack.cjs": "nodejs/setup-corepack.cjs" } } diff --git a/plugins/nodejs/setup-corepack.js b/plugins/nodejs/setup-corepack.cjs similarity index 87% rename from plugins/nodejs/setup-corepack.js rename to plugins/nodejs/setup-corepack.cjs index f2471067895..6904ffa8e21 100644 --- a/plugins/nodejs/setup-corepack.js +++ b/plugins/nodejs/setup-corepack.cjs @@ -1,5 +1,9 @@ // Configures Corepack for the Devbox shell. This is the nodejs plugin's -// init_hook, invoked as: node setup-corepack.js +// init_hook, invoked as: node setup-corepack.cjs +// +// The .cjs extension forces CommonJS regardless of the project's package.json, +// which may declare "type": "module" (otherwise the require() calls below fail +// with "require is not defined in ES module scope"). See issue #2856. // // It is a no-op unless DEVBOX_COREPACK_ENABLED is set, in which case it: // 1. Enables Corepack, installing its package-manager shims into the diff --git a/testscripts/plugin/nodejs_corepack_autodetect.test.txt b/testscripts/plugin/nodejs_corepack_autodetect.test.txt index ed10396b30b..3d132b5edb7 100644 --- a/testscripts/plugin/nodejs_corepack_autodetect.test.txt +++ b/testscripts/plugin/nodejs_corepack_autodetect.test.txt @@ -23,8 +23,21 @@ cp package-no-pkgmgr.json package.json exec devbox run -- node -e 'console.log("case2-ok")' stdout 'case2-ok' +# Case 3: package.json declares "type": "module" (issue #2856). +# The setup-corepack script must still load (it uses a .cjs extension so it is +# always treated as CommonJS) and shell init must still succeed. +cp package-esm.json package.json +exec devbox run -- node -e 'console.log("case3-ok")' +stdout 'case3-ok' + -- package-no-pkgmgr.json -- { "name": "nodejs-corepack-autodetect", "version": "1.0.0" } +-- package-esm.json -- +{ + "name": "nodejs-corepack-autodetect", + "version": "1.0.0", + "type": "module" +}