Skip to content

fix(cli): show actionable error when package.json is missing "type": "module"#76

Open
krismuhi wants to merge 1 commit intomainfrom
fix/cli-esm-error-message
Open

fix(cli): show actionable error when package.json is missing "type": "module"#76
krismuhi wants to merge 1 commit intomainfrom
fix/cli-esm-error-message

Conversation

@krismuhi
Copy link
Copy Markdown
Member

@krismuhi krismuhi commented May 2, 2026

Summary

  • Bug: running mobilewright test with a CJS package.json (missing "type": "module") dumps a raw Node.js ERR_REQUIRE_ESM stack trace with no hint about the fix.
  • Fix: catch ERR_REQUIRE_ESM in the config-loading step and print a clear message telling the user exactly what to add to their package.json.
  • Affects: any user who scaffolds a new project without "type": "module" or copies config from a CJS project.

Error before this fix

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/mobilewright/dist/index.js
from .../mobilewright.config.ts not supported.
Instead change the require of index.js in .../mobilewright.config.ts to a dynamic import()
which is available in all CommonJS modules.
    at Object.newLoader2 [as .js] (.../playwright/lib/third_party/pirates.js:52:22)
    ...

Error after this fix

Error: Could not load your mobilewright config.

Your config file imports an ES Module (e.g. mobilewright), but your package.json
is missing "type": "module". Node's CommonJS loader cannot require() ES Modules.

Fix: add the following to your package.json:

  "type": "module"

Then re-run: npx mobilewright test

Test plan

  • Verify existing tests pass (npm test — 147 passed, 1 skipped)
  • Manually test with a project missing "type": "module" to confirm the friendly error is shown
  • Verify that non-ERR_REQUIRE_ESM errors are still re-thrown as before

…"module"

Catch ERR_REQUIRE_ESM thrown by loadConfigFromFile and print a clear,
human-readable message telling the user exactly what to add to their
package.json, instead of dumping a raw Node.js internal stack trace.
@krismuhi krismuhi force-pushed the fix/cli-esm-error-message branch from e0440a8 to 8ce1010 Compare May 3, 2026 17:45
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.

2 participants