Skip to content

fix(nodejs plugin): rewrite setup-corepack as an ESM .mjs module#2857

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

fix(nodejs plugin): rewrite setup-corepack as an ESM .mjs module#2857
mikeland73 wants to merge 2 commits into
mainfrom
claude/focused-goldberg-eAYeU

Conversation

@mikeland73

@mikeland73 mikeland73 commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2856.

The Corepack setup script introduced in #2845 (setup-corepack.js) is written in CommonJS and uses require(). The nodejs plugin runs it as the shell init_hook:

node "{{ .Virtenv }}/bin/setup-corepack.js"

When a project's root package.json sets "type": "module", Node treats every .js file in the directory tree — including this one inside .devbox/virtenv — as an ES module. The script then crashes on its first require(), taking the whole shell down:

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension
and '.../package.json' contains "type": "module".

As a result, devbox shell no longer starts for any nodejs project whose package.json declares "type": "module".

Fix

Rewrite the script as a native ES module and name it setup-corepack.mjs. The .mjs extension forces Node to treat the file as ESM regardless of the surrounding package.json type, so it works for both "type": "module" projects and default/CommonJS projects. (A plain .js ESM file would break the inverse case — Node parses .js as CommonJS when type is absent or commonjs.)

Changes:

  • Rename plugins/nodejs/setup-corepack.jsplugins/nodejs/setup-corepack.mjs.
  • require()import for node:child_process and node:path.
  • Read package.json via readFileSync + JSON.parse instead of require(), since JSON module import syntax differs across Node versions.
  • Update the plugin's init_hook and create_files references in plugins/nodejs.json.
  • Add a regression test case ("type": "module" in package.json) to testscripts/plugin/nodejs_corepack_autodetect.test.txt.

How was it tested?

  • node --check on the .mjs file (valid ESM syntax).
  • Ran the script directly in scratch dirs and confirmed it exits 0 (no crash, shell init never breaks) in all of:
    • package.json with {"type":"module"} — previously the reported ReferenceError.
    • default/CommonJS package.json.
    • package.json with a pinned packageManager (pnpm@9.0.0) — autodetect path activates correctly.
  • Added Case 3 to the corepack autodetect testscript to guard against regression.

cc @AdaptivChris (issue reporter)


Generated by Claude Code

…:"module"

The corepack setup script added in #2845 uses CommonJS `require()`, but was
named setup-corepack.js. When a project's root package.json sets
`"type": "module"`, Node treats every `.js` file in the tree as an ES module,
so running the script crashed the shell with:

    ReferenceError: require is not defined in ES module scope

Rename the script to setup-corepack.cjs (and update the plugin's init_hook and
create_files references) so Node always treats it as CommonJS regardless of the
project's package.json type. Bump the plugin version 0.0.3 -> 0.0.4.

Adds a regression test covering a package.json with "type": "module".

Fixes #2856
Copilot AI review requested due to automatic review settings June 8, 2026 14:06

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 a Node.js module-type edge case in the nodejs plugin: when a project’s root package.json sets "type": "module", Node treats setup-corepack.js under .devbox/virtenv/... as ESM and crashes on require(), breaking devbox shell. The fix renames the init hook script to .cjs so Node always executes it as CommonJS.

Changes:

  • Renamed the Corepack init hook script to setup-corepack.cjs to avoid ESM interpretation under "type": "module".
  • Updated plugins/nodejs.json to reference the new .cjs filename and bumped plugin version 0.0.30.0.4.
  • Added a regression test case covering "type": "module" in package.json.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
plugins/nodejs/setup-corepack.cjs Updates script header/docs to reflect .cjs usage so CommonJS require() remains valid under ESM projects.
plugins/nodejs.json Points init_hook and create_files at setup-corepack.cjs and bumps plugin version.
testscripts/plugin/nodejs_corepack_autodetect.test.txt Adds a regression case to ensure shells still start when package.json declares "type": "module".

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Rewrites the corepack setup script as a native ES module and names it
setup-corepack.mjs. The .mjs extension forces Node to treat it as ESM
regardless of the project's package.json "type" field, so it no longer
crashes with "require is not defined in ES module scope" when a project sets
"type": "module" (issue #2856), and it also works for default CommonJS
projects.

- require() -> import for child_process and path.
- Read package.json via readFileSync + JSON.parse instead of require(), since
  JSON module import syntax varies across Node versions.
- Update the plugin's init_hook and create_files references to the .mjs file.

Verified the script exits 0 (no crash) for "type":"module", default/CommonJS,
and pinned-packageManager projects.
@mikeland73 mikeland73 changed the title fix(nodejs plugin): use .cjs for setup-corepack so it survives "type":"module" fix(nodejs plugin): rewrite setup-corepack as an ESM .mjs module Jun 8, 2026
@mikeland73 mikeland73 requested a review from Lagoja June 8, 2026 14:39
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