Skip to content

fix: run Node.js version check before loading bundle (#11546)#11547

Merged
zkochan merged 1 commit into
mainfrom
fix/11546
May 8, 2026
Merged

fix: run Node.js version check before loading bundle (#11546)#11547
zkochan merged 1 commit into
mainfrom
fix/11546

Conversation

@zkochan

@zkochan zkochan commented May 8, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #11546.

The Node.js version check in pnpm/bin/pnpm.mjs was effectively dead code. It used a static import to load the bundled dist/pnpm.mjs, and ESM hoists static imports — so the bundle was parsed before the if (major < 22 ...) block could run. On Node.js versions older than the bundle's syntax target (e.g. Node 18 hitting a regex with the /v flag in a transitive dependency), users got a confusing SyntaxError: Invalid regular expression flags instead of the intended ERROR: This version of pnpm requires at least Node.js v22.13 message.

Switching to await import('../dist/pnpm.mjs') makes the load order match the source order, so the version check runs first and exits with a friendly error.

The other entry points are unaffected:

  • pnpm/bin/pnpm.cjs (Corepack shim) already uses dynamic import('./pnpm.mjs') and goes through the fixed file.
  • pnpm/pnpm.cjs (@pnpm/exe SEA entry) ships its own embedded Node.js, so the version check isn't needed there.

Test plan

  • Reproduced the hoisting behavior with a minimal ESM repro: a static import of a file containing /a/v parses before any code in the importing module runs, even when an early process.exit(1) should have prevented it.
  • Verified the fix: with the dynamic await import(), the version-check console.error + process.exit(1) runs first and the bundle is never parsed.
  • Manual smoke test: pnpm --version on a supported Node.js (24+) still works.

Written by an agent (Claude Code, claude-opus-4-7).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a crash during pnpm CLI startup on unsupported Node.js versions that displayed a confusing syntax error instead of a clear compatibility message.

The static `import` of `dist/pnpm.mjs` in `bin/pnpm.mjs` was hoisted by
the ES module loader and parsed before the version check below it,
causing pnpm to crash with `SyntaxError: Invalid regular expression
flags` on Node.js versions older than the bundle's syntax target instead
of printing a clear "requires Node.js v22.13" error. Switching to a
dynamic `await import()` lets the version check run first.
Copilot AI review requested due to automatic review settings May 8, 2026 14:12
@coderabbitai

coderabbitai Bot commented May 8, 2026

Copy link
Copy Markdown

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3b0feb00-c494-4e56-8840-255af76af5f0

📥 Commits

Reviewing files that changed from the base of the PR and between 6925be3 and 347fa3c.

📒 Files selected for processing (2)
  • .changeset/pnpm-bin-version-check-hoisting.md
  • pnpm/bin/pnpm.mjs

📝 Walkthrough

Walkthrough

pnpm CLI startup is fixed by deferring the bundled module import until after Node.js version compatibility checks complete, preventing parse-time SyntaxError crashes from unsupported regex syntax (/v flag) on Node 18.

Changes

CLI Version Check and Import Order

Layer / File(s) Summary
CLI Entry Point Fix
pnpm/bin/pnpm.mjs
Static import of ../dist/pnpm.mjs is replaced with dynamic await import() placed after Node.js version check, preventing older runtimes from parsing modules containing modern regex syntax.
Release Documentation
.changeset/pnpm-bin-version-check-hoisting.md
Changeset entry documents the patch fix for the crash on unsupported Node.js versions and explains the hoisting problem that caused parse errors before the compatibility check could run.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A bundle that wouldn't compile,
Made parsing fail with style,
But dynamic imports save the day,
Check the version first, I say,
Then load the code the proper way. 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: run Node.js version check before loading bundle' clearly and specifically summarizes the main change - it describes the fix and the core objective of ensuring the version check runs before bundle loading.
Linked Issues check ✅ Passed The PR fulfills the objective from issue #11546 by replacing static import with dynamic await import() in pnpm/bin/pnpm.mjs to ensure Node.js version checks execute before bundle parsing, preventing the 'Invalid regular expression flags' error.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #11546: the changeset file documents the fix, and pnpm/bin/pnpm.mjs is modified to defer bundled code loading until after version checks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/11546

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Fixes a pnpm CLI startup failure on unsupported Node.js versions by ensuring the Node.js version guard runs before the bundled CLI module is parsed/loaded, avoiding confusing syntax errors from newer syntax in bundled dependencies.

Changes:

  • Replaced a hoisted static ESM import with a dynamic await import() in pnpm/bin/pnpm.mjs so the version check executes first.
  • Added a changeset documenting the user-facing fix for the incorrect SyntaxError behavior on older Node.js versions.

Reviewed changes

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

File Description
pnpm/bin/pnpm.mjs Switches bundle loading to dynamic import to prevent pre-check parsing on unsupported Node.js versions.
.changeset/pnpm-bin-version-check-hoisting.md Adds a patch changeset describing the crash fix and linking to the reported issue.

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

@zkochan zkochan merged commit 2b267a7 into main May 8, 2026
17 checks passed
@zkochan zkochan deleted the fix/11546 branch May 8, 2026 15:07
zkochan added a commit that referenced this pull request May 8, 2026
The static `import` of `dist/pnpm.mjs` in `bin/pnpm.mjs` was hoisted by
the ES module loader and parsed before the version check below it,
causing pnpm to crash with `SyntaxError: Invalid regular expression
flags` on Node.js versions older than the bundle's syntax target instead
of printing a clear "requires Node.js v22.13" error. Switching to a
dynamic `await import()` lets the version check run first.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unexpected error: Invalid regular expression flags

2 participants