Skip to content

module: fix extensionless CJS files in type:module packages#62083

Open
mcollina wants to merge 2 commits intonodejs:mainfrom
mcollina:fix/extensionless-cjs-in-type-module
Open

module: fix extensionless CJS files in type:module packages#62083
mcollina wants to merge 2 commits intonodejs:mainfrom
mcollina:fix/extensionless-cjs-in-type-module

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Mar 3, 2026

Summary

  • PR module: fix extensionless entry with explicit type=commonjs #61600 made the CJS loader respect package.json type field for extensionless files, but also enforced type: "module" which breaks CJS extensionless files inside ESM packages (e.g. yargs v17)
  • Only enforce type: "commonjs" for extensionless files; for type: "module", leave format undefined so V8's syntax detection determines the correct format
  • Adds regression test that require()s a CJS extensionless file from a type: "module" package and verifies exports are correct

Test plan

  • Existing test test/es-module/test-extensionless-esm-type-commonjs.js still passes (ESM syntax in type: "commonjs" still throws SyntaxError)
  • New test case verifies CJS extensionless file in type: "module" package returns correct exports via require()
  • Manual verification: require() of fixture returns { hello: 'world' } (was returning empty ESM namespace before fix)

Fixes: #61971
Refs: yargs/yargs#2509
Refs: #61600

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Mar 3, 2026
PR nodejs#61600 made the CJS loader respect the package.json
type field for extensionless files, but also enforced
type: "module" which breaks CJS extensionless files
inside ESM packages (e.g. yargs v17).

Only enforce type: "commonjs" for extensionless files.
For type: "module", leave format undefined so V8's
syntax detection determines the correct format,
allowing CJS extensionless files in ESM packages to
continue working.

Fixes: nodejs#61971
Refs: yargs/yargs#2509
@mcollina mcollina force-pushed the fix/extensionless-cjs-in-type-module branch from 3e6aa5a to 6e3ed67 Compare March 3, 2026 10:37
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.65%. Comparing base (e78bf55) to head (519235b).
⚠️ Report is 16 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #62083   +/-   ##
=======================================
  Coverage   89.64%   89.65%           
=======================================
  Files         676      676           
  Lines      206249   206326   +77     
  Branches    39518    39527    +9     
=======================================
+ Hits       184892   184980   +88     
+ Misses      13479    13471    -8     
+ Partials     7878     7875    -3     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 98.14% <100.00%> (-0.22%) ⬇️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

Code LGTM with a correction on comment

Co-authored-by: Joyee Cheung <joyeec9h3@gmail.com>
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 3, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 3, 2026
@nodejs-github-bot
Copy link
Collaborator

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

Labels

module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v25.7] yargs v17 compatibility issue

5 participants