Skip to content

module: fix extensionless entry with explicit type=commonjs#61600

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
inoway46:fix-extensionless-type-commonjs-esm
Feb 16, 2026
Merged

module: fix extensionless entry with explicit type=commonjs#61600
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
inoway46:fix-extensionless-type-commonjs-esm

Conversation

@inoway46
Copy link
Copy Markdown
Contributor

@inoway46 inoway46 commented Jan 31, 2026

When an extensionless entry point contains ESM syntax but is in a package with "type": "commonjs" in package.json, the module would silently exit with code 0 without executing or showing any error. This happened because extensionless files skip the .js suffix check in the CJS loader, so the explicit type: commonjs was not being enforced, allowing ESM syntax to be silently delegated to ESM loading which never completed before the process exited.

This change ensures the CJS loader treats extensionless entry points as commonjs when type is explicitly set to "commonjs" in package.json, forcing ESM syntax to surface as a SyntaxError instead of silently exiting.

Fixes: #61104

Related: #61171 (alternative approach)

@nodejs-github-bot
Copy link
Copy Markdown
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 Jan 31, 2026
@inoway46 inoway46 marked this pull request as ready for review January 31, 2026 15:45
Copy link
Copy Markdown
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.

Thanks for the PR, a couple of comments, otherwise this looks good.

Comment thread test/es-module/test-extensionless-esm-type-commonjs.js Outdated
Comment thread test/es-module/test-extensionless-esm-type-commonjs.js Outdated
Comment thread lib/internal/modules/cjs/loader.js Outdated
@inoway46 inoway46 requested a review from joyeecheung February 1, 2026 04:06
Comment thread test/es-module/test-extensionless-esm-type-commonjs.js Outdated
@inoway46 inoway46 requested a review from joyeecheung February 1, 2026 17:12
Comment thread test/es-module/test-extensionless-esm-type-commonjs.js Outdated
@inoway46 inoway46 requested a review from joyeecheung February 6, 2026 16:17
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 7, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 7, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.74%. Comparing base (f6464c5) to head (79d60d5).
⚠️ Report is 174 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61600      +/-   ##
==========================================
- Coverage   89.76%   89.74%   -0.03%     
==========================================
  Files         673      675       +2     
  Lines      203944   204538     +594     
  Branches    39191    39307     +116     
==========================================
+ Hits       183080   183553     +473     
- Misses      13194    13297     +103     
- Partials     7670     7688      +18     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 98.36% <100.00%> (+0.19%) ⬆️

... and 63 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.

@inoway46
Copy link
Copy Markdown
Contributor Author

inoway46 commented Feb 7, 2026

Thanks for the approval.
I pushed a small lint follow-up: I added require('../common') at the top of test/es-module/test-extensionless-esm-type-commonjs.js to satisfy the required module ordering rules.

I also verified make lint-js locally for the changed JS files.
Could you please rerun CI if needed? Thank you.

Comment thread lib/internal/modules/cjs/loader.js
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 8, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@aduh95 aduh95 added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 8, 2026
@joyeecheung joyeecheung added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Feb 8, 2026
@joyeecheung
Copy link
Copy Markdown
Member

@inoway46 Can you squash the commits? I don't think they would pass the test individually.

mcollina added a commit to mcollina/node that referenced this pull request Mar 9, 2026
PR nodejs#61600 made the CJS loader respect the package.json type field for
extensionless files, which fixed nodejs#61104 but also enforced type: "module"
for extensionless files. This broke CJS extensionless files inside ESM
packages (e.g. yargs v17).

Only enforce type: "commonjs" for extensionless files. For type: "module",
leave format undefined so syntax detection handles it. This restores the
documented exception in the CJS documentation that has existed since v16:
extensionless files in type: "module" packages are recognized as CJS when
included via require().

Fixes: nodejs#61971
Refs: yargs/yargs#2509
Refs: nodejs#61600
PR-URL: nodejs#62083
mcollina added a commit to mcollina/node that referenced this pull request Mar 10, 2026
Remove the documented exception that extensionless files in
type: "module" packages are recognized as CommonJS when included
via require(). This exception conflicted with the ESM resolution
specification which states that extensionless files within a
package scope with an explicit type field follow the format of
the type field.

The behavior on main already matches the ESM spec since nodejs#61600,
this change aligns the CJS documentation accordingly.

Refs: nodejs#61600
Refs: nodejs#62083
mcollina added a commit to mcollina/node that referenced this pull request Mar 10, 2026
PR nodejs#61600 made the CJS loader respect the package.json
type field for extensionless files, which fixed nodejs#61104
but also enforced type: "module" for extensionless
files. This broke CJS extensionless files inside ESM
packages (e.g. yargs v17).

Only enforce type: "commonjs" for extensionless files.
For type: "module", leave format undefined so syntax
detection handles it. This restores the documented
exception in the CJS documentation that has existed
since v16: extensionless files in type: "module"
packages are recognized as CJS when included via
require().

Fixes: nodejs#61971
Refs: yargs/yargs#2509
Refs: nodejs#61600
PR-URL: nodejs#62083
aduh95 pushed a commit that referenced this pull request Mar 10, 2026
24b8585 made the CJS loader respect the `package.json`
type field for extensionless files, which fixed #61104
but also enforced `"type": "module"` for extensionless
files. This broke CJS extensionless files inside ESM
packages (e.g. yargs v17).

Only enforce `"type": "commonjs"` for extensionless files.
For `"type": "module"`, leave format undefined so syntax
detection handles it. This restores the documented
exception in the CJS documentation that has existed
since v16: extensionless files in `"type": "module"`
packages are recognized as CJS when included via
`require()`.

Fixes: #61971
Refs: yargs/yargs#2509
Refs: #61600
PR-URL: #62083
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@wissamblue69-dotcom

This comment was marked as off-topic.

@wissamblue69-dotcom

This comment was marked as off-topic.

@wissamblue69-dotcom

This comment was marked as off-topic.

nodejs-github-bot pushed a commit that referenced this pull request Apr 8, 2026
Remove the documented exception that extensionless files in
type: "module" packages are recognized as CommonJS when included
via require(). This exception conflicted with the ESM resolution
specification which states that extensionless files within a
package scope with an explicit type field follow the format of
the type field.

The behavior on main already matches the ESM spec since #61600,
this change aligns the CJS documentation accordingly.

Refs: #61600
Refs: #62083
PR-URL: #62176
Refs: #61600
Refs: #62083
Refs: #61971
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
RafaelGSS pushed a commit that referenced this pull request Apr 14, 2026
Remove the documented exception that extensionless files in
type: "module" packages are recognized as CommonJS when included
via require(). This exception conflicted with the ESM resolution
specification which states that extensionless files within a
package scope with an explicit type field follow the format of
the type field.

The behavior on main already matches the ESM spec since #61600,
this change aligns the CJS documentation accordingly.

Refs: #61600
Refs: #62083
PR-URL: #62176
Refs: #61600
Refs: #62083
Refs: #61971
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
kevinoid added a commit to kevinoid/node-project-template that referenced this pull request May 16, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
@wesleytodd wesleytodd mentioned this pull request May 22, 2026
2 tasks
kevinoid added a commit to kevinoid/node-project-template that referenced this pull request May 26, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/appveyor-status that referenced this pull request May 26, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/dotnet-identifier-case that referenced this pull request May 26, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/fetch-procore-api-docs that referenced this pull request May 26, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/git-branch-is that referenced this pull request May 26, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/hub-ci-status that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/inflate-auto that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/json-replace-exponentials that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/json-schema-intersect that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/json-stringify-raw that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/mocha-ur2ue that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/nodecat that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/noderegression that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/openapi-transformer-base that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/openapi-transformer-cli that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/openapi-transformer-pipeline that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/openapi-transformers that referenced this pull request May 27, 2026
To work around a crash on Node 25/26
(<nodejs/node#62083>) caused by an extensionless
`./yargs` file in yargs@17 (<nodejs/node#62083>)
which started being interpreted as `type: "module"` in Node.js 25.7 due
to <nodejs/node#61600>, which was reverted by
<nodejs/node#62083> for Node.js 25, but retained
for Node.js 26.

Work around the issue by overriding the c8 yargs dependency to use
yargs@^18.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. dont-land-on-v24.x PRs that should not land on the v24.x-staging branch and should not be released in v24.x. 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.

With "type": "commonjs", #!/usr/bin/env node file does not execute but returns success and no error messages when the file is ESM

8 participants