Skip to content

Fix nodejs plugin corepack setup failing in "type": "module" projects#2873

Open
mikeland73 wants to merge 2 commits into
mainfrom
claude/focused-goldberg-ro2sgf
Open

Fix nodejs plugin corepack setup failing in "type": "module" projects#2873
mikeland73 wants to merge 2 commits into
mainfrom
claude/focused-goldberg-ro2sgf

Conversation

@mikeland73

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2856.

The nodejs plugin runs a Corepack setup script as its init_hook. The script uses CommonJS require(), but it was named setup-corepack.js. When a project's root package.json declares "type": "module", Node treats every .js file in that package — including this one — as an ES module, so the script crashes with:

ReferenceError: require is not defined in ES module scope, you can use import instead

This breaks devbox shell entirely for any Node project using ES modules, which is increasingly common.

Fix

Rename the script to setup-corepack.cjs. The .cjs extension forces Node to always interpret it as CommonJS, regardless of the surrounding package.json type field. The plugin's init_hook and create_files references are updated accordingly, and the plugin version is bumped 0.0.30.0.4.

Testing

  • Added a third case to testscripts/plugin/nodejs_corepack_autodetect.test.txt that copies a package.json declaring "type": "module" and verifies shell init still succeeds.
  • Verified at the Node level that a .js file with require() fails under "type": "module" while the equivalent .cjs file 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

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
Copilot AI review requested due to automatic review settings June 17, 2026 14:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .cjs filename so Node treats it as CommonJS under ESM projects.
  • Added a regression test case covering "type": "module" package.json.
  • Bumped the nodejs plugin version from 0.0.3 to 0.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

Copy link
Copy Markdown
Collaborator Author

Heads-up on CI: the only failing check (test (not-main, ubuntu-latest, project-tests-only)) is failing on TestExamples/development_elixir_elixir_hello_run_test.test, which is unrelated to this change. It fails while installing Hex from the network:

** (Mix) httpc request failed with: {:failed_connect, [{:to_address, {~c"builds.hex.pm", 443}}, {:inet, [:inet], {:tls_alert, {:unsupported_certificate, ...}}}]}
Could not install Hex because Mix could not download metadata at https://builds.hex.pm/installs/hex-1.x.csv.

This is an environmental/network flake reaching builds.hex.pm, not a code regression. All Node.js / Corepack example and plugin tests (the actual subject of this PR) pass. It reproduced on a re-run; I don't have permission to re-run jobs via the API, so a maintainer re-run should clear it.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

setup-corepack.js fails when "module" is specified in root package.json

3 participants