-
Notifications
You must be signed in to change notification settings - Fork 182
Description
Summary
The magnitude-test CLI command npx magnitude init fails with an ES module compatibility error when trying to load the open package. This prevents new
users from initializing Magnitude test projects.
Environment
- Node.js: 20.11.0 (also tested with 20.18.1)
- npm: 10.3.0
- Platform: macOS (Darwin 24.5.0)
- magnitude-test version: 0.3.0
- magnitude-core version: 0.2.21
Error Details
Command that fails:
npx magnitude init
Error message:
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/magnitude-core/node_modules/open/index.js from
/path/to/node_modules/magnitude-core/dist/index.cjs not supported.
Instead change the require of index.js in /path/to/node_modules/magnitude-core/dist/index.cjs to a dynamic import() which is available in all
CommonJS modules.
Full stack trace location:
at Object. (/path/to/node_modules/magnitude-core/dist/index.cjs:21:12)
Root Cause Analysis
The issue stems from a module system compatibility problem:
- magnitude-test has "type": "module" in its package.json and its CLI (dist/cli.js) uses ES module syntax (import statements)
- magnitude-core uses CommonJS and attempts to load the open package with require('open') at line 21 of dist/index.cjs
- open package version 9.0.0+ is ES module only and cannot be loaded with CommonJS require()
- When running in the ES module context of magnitude-test, Node.js expects all dependencies to be ES module compatible
Code Location
The problematic code is in magnitude-core/dist/index.cjs:21:
var open = require('open');
This is later used in the Claude Code authentication flow:
await open(authUrl);
Why This Only Affects magnitude-test
- Direct usage of magnitude-core as a CommonJS library works fine
- The issue only occurs when magnitude-core is loaded from within the ES module context of magnitude-test CLI
- This explains why other projects using magnitude-core don't experience this issue
Temporary Workaround
Users can temporarily fix this by downgrading the open package in magnitude-core:
cd node_modules/magnitude-core
npm install [email protected] --save
However, this is not a sustainable solution for end users.
Suggested Solutions
Option 1: Pin open dependency (Quick fix)
In magnitude-core/package.json, pin the open dependency to a CommonJS-compatible version:
{
"dependencies": {
"open": "^8.4.2"
}
}
Option 2: Use dynamic imports (Better long-term)
Replace the CommonJS require with dynamic import in magnitude-core:
// Replace this:
var open = require('open');
// With this:
const open = null;
const getOpen = async () => {
if (!open) {
const module = await import('open');
return module.default;
}
return open;
};
// Then use it as:
const openFn = await getOpen();
await openFn(authUrl);
Option 3: Proper ES module support
Update magnitude-core to properly support both CommonJS and ES modules with appropriate build configuration.
Impact
This is a blocking issue for new users trying to get started with Magnitude, as npx magnitude init is the primary way to initialize a new test
project. It affects the entire onboarding experience.
Additional Context
- The magnitude-demo-repo works because it doesn't use the CLI initialization
- The issue is reproducible across different Node.js versions (tested 20.11.0 and 20.18.1)
- This appears to be a recent regression, likely introduced when the open package was updated to v9.0.0+
Reproduction Steps
- Create a new directory: mkdir test-magnitude && cd test-magnitude
- Initialize npm: npm init -y
- Install magnitude-test: npm install magnitude-test
- Run: npx magnitude init
- Observe the ES module error
Expected behavior: npx magnitude init should successfully create the test directory structure and install dependencies.
Actual behavior: Command fails with ES module compatibility error.