Fix nodejs plugin corepack setup failing in "type": "module" projects#2873
Fix nodejs plugin corepack setup failing in "type": "module" projects#2873mikeland73 wants to merge 2 commits into
Conversation
The nodejs plugin's corepack init_hook script uses CommonJS `require()`, but it was named `setup-corepack.js`. When the project's root `package.json` declares `"type": "module"`, Node treats the `.js` file as an ES module and fails with "require is not defined in ES module scope", breaking `devbox shell` entirely. Rename the script to `setup-corepack.cjs` so it is always interpreted as CommonJS regardless of the project's package.json `type` field, and bump the plugin version. Adds a testscript case covering a project that declares `"type": "module"`. Fixes #2856 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015e5foES7T8gDQRHK3hJSdH
There was a problem hiding this comment.
Pull request overview
This PR fixes the Node.js plugin’s Corepack init hook failing in projects whose root package.json declares "type": "module", by ensuring the Corepack setup script is always executed as CommonJS.
Changes:
- Switched the Corepack setup script invocation and file creation to use a
.cjsfilename so Node treats it as CommonJS under ESM projects. - Added a regression test case covering
"type": "module"package.json. - Bumped the
nodejsplugin version from0.0.3to0.0.4.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
testscripts/plugin/nodejs_corepack_autodetect.test.txt |
Adds a new test case to verify the Corepack init hook doesn’t break when package.json sets "type": "module". |
plugins/nodejs/setup-corepack.cjs |
Documents why the Corepack setup script must be .cjs to remain CommonJS in ESM projects. |
plugins/nodejs.json |
Updates init_hook and create_files paths to .cjs and bumps the plugin version. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015e5foES7T8gDQRHK3hJSdH
|
Heads-up on CI: the only failing check ( This is an environmental/network flake reaching Generated by Claude Code |
Summary
Fixes #2856.
The
nodejsplugin runs a Corepack setup script as itsinit_hook. The script uses CommonJSrequire(), but it was namedsetup-corepack.js. When a project's rootpackage.jsondeclares"type": "module", Node treats every.jsfile in that package — including this one — as an ES module, so the script crashes with:This breaks
devbox shellentirely for any Node project using ES modules, which is increasingly common.Fix
Rename the script to
setup-corepack.cjs. The.cjsextension forces Node to always interpret it as CommonJS, regardless of the surroundingpackage.jsontypefield. The plugin'sinit_hookandcreate_filesreferences are updated accordingly, and the plugin version is bumped0.0.3→0.0.4.Testing
testscripts/plugin/nodejs_corepack_autodetect.test.txtthat copies apackage.jsondeclaring"type": "module"and verifies shell init still succeeds..jsfile withrequire()fails under"type": "module"while the equivalent.cjsfile loads correctly.Notes
cc @AdaptivChris (issue reporter) — thanks for the clear reproduction.
🤖 Generated with Claude Code
https://claude.ai/code/session_015e5foES7T8gDQRHK3hJSdH
Generated by Claude Code