Commit 691c528d950 for nodejs
commit 691c528d95028e8c2ea7d02994abe828b36f4933
Author: James M Snell <jasnell@gmail.com>
Date: Sun Nov 15 16:01:39 2026 +0000
module: move DEP0128 to End-of-Life
A package.json whose "main" entry cannot be resolved no longer falls
back to the package's top-level index file when loaded with require().
Instead, a MODULE_NOT_FOUND error is thrown, matching the existing
behavior for packages that have no index file.
This has been a runtime deprecation since v16.0.0.
Refs: https://github.com/nodejs/node/pull/37204
Refs: https://github.com/nodejs/node/pull/26823
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67487
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md
index dbe46c36b11..9f0afb750a9 100644
--- a/doc/api/deprecations.md
+++ b/doc/api/deprecations.md
@@ -2942,6 +2942,9 @@ npx codemod @nodejs/timers-deprecations
<!-- YAML
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/67487
+ description: End-of-Life.
- version: v16.0.0
pr-url: https://github.com/nodejs/node/pull/37204
description: Runtime deprecation.
@@ -2950,12 +2953,12 @@ changes:
description: Documentation-only.
-->
-Type: Runtime
+Type: End-of-Life
-Modules that have an invalid `main` entry (e.g., `./does-not-exist.js`) and
-also have an `index.js` file in the top level directory will resolve the
-`index.js` file. That is deprecated and is going to throw an error in future
-Node.js versions.
+Modules that have an invalid `main` entry (e.g., `./does-not-exist.js`) no
+longer fall back to resolving an `index.js` file in the top level directory.
+Loading such a module with `require()` now throws a `MODULE_NOT_FOUND` error.
+Fix the `main` entry so that it points to an existing file.
### DEP0129: `ChildProcess._channel`
diff --git a/doc/api/modules.md b/doc/api/modules.md
index 01a5343e92d..a0ce91d9f7f 100644
--- a/doc/api/modules.md
+++ b/doc/api/modules.md
@@ -396,8 +396,7 @@ LOAD_AS_DIRECTORY(X)
c. let M = X + (json main field)
d. LOAD_AS_FILE(M)
e. LOAD_INDEX(M)
- f. LOAD_INDEX(X) DEPRECATED
- g. THROW "not found"
+ f. THROW "not found"
2. LOAD_INDEX(X)
LOAD_NODE_MODULES(X, START)
@@ -674,14 +673,18 @@ If this was in a folder at `./some-library`, then
`./some-library/lib/some-library.js`.
If there is no [`package.json`][] file present in the directory, or if the
-[`"main"`][] entry is missing or cannot be resolved, then Node.js
-will attempt to load an `index.js` or `index.node` file out of that
-directory. For example, if there was no [`package.json`][] file in the previous
-example, then `require('./some-library')` would attempt to load:
+[`"main"`][] entry is missing, then Node.js will attempt to load an `index.js`
+or `index.node` file out of that directory. For example, if there was no
+[`package.json`][] file in the previous example, then
+`require('./some-library')` would attempt to load:
* `./some-library/index.js`
* `./some-library/index.node`
+If the [`"main"`][] entry is present but cannot be resolved, Node.js does not
+fall back to `index.js` or `index.node` and instead throws a
+`MODULE_NOT_FOUND` error.
+
If these attempts fail, then Node.js will report the entire module as missing
with the default error:
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index f7c08348f3d..94d13c47bd5 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -560,30 +560,20 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
}
const filename = path.resolve(requestPath, pkg);
- let actual = tryFile(filename, isMain) ||
+ const actual = tryFile(filename, isMain) ||
tryExtensions(filename, exts, isMain) ||
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
- if (actual === false) {
- actual = tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
- if (!actual) {
- // eslint-disable-next-line no-restricted-syntax
- const err = new Error(
- `Cannot find module '${filename}'. ` +
- 'Please verify that the package.json has a valid "main" entry',
- );
- err.code = 'MODULE_NOT_FOUND';
- err.path = pjsonPath;
- err.requestPath = originalPath;
- // TODO(BridgeAR): Add the requireStack as well.
- throw err;
- } else {
- process.emitWarning(
- `Invalid 'main' field in '${pjsonPath}' of '${pkg}'. ` +
- 'Please either fix that or report it to the module author',
- 'DeprecationWarning',
- 'DEP0128',
- );
- }
+ if (!actual) {
+ // eslint-disable-next-line no-restricted-syntax
+ const err = new Error(
+ `Cannot find module '${filename}'. ` +
+ 'Please verify that the package.json has a valid "main" entry',
+ );
+ err.code = 'MODULE_NOT_FOUND';
+ err.path = pjsonPath;
+ err.requestPath = originalPath;
+ // TODO(BridgeAR): Add the requireStack as well.
+ throw err;
}
return actual;
}
diff --git a/test/parallel/test-module-loading-deprecated.js b/test/parallel/test-module-loading-deprecated.js
deleted file mode 100644
index 3b43284aec7..00000000000
--- a/test/parallel/test-module-loading-deprecated.js
+++ /dev/null
@@ -1,10 +0,0 @@
-'use strict';
-
-const common = require('../common');
-const assert = require('assert');
-
-common.expectWarning('DeprecationWarning', {
- DEP0128: /^Invalid 'main' field in '.+main[/\\]package\.json' of 'doesnotexist\.js'\..+module author/
-});
-
-assert.strictEqual(require('../fixtures/packages/missing-main').ok, 'ok');
diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js
index 1e2efc6fd26..40e3a2eb45d 100644
--- a/test/sequential/test-module-loading.js
+++ b/test/sequential/test-module-loading.js
@@ -29,7 +29,7 @@ const path = require('path');
const backslash = /\\/g;
-process.on('warning', common.mustCall());
+process.on('warning', common.mustNotCall());
console.error('load test-module-loading.js');
@@ -107,14 +107,16 @@ assert.strictEqual(require('../fixtures/packages/index').ok, 'ok');
assert.strictEqual(require('../fixtures/packages/main').ok, 'ok');
assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok');
-common.expectWarning(
- 'DeprecationWarning',
- "Invalid 'main' field in '" +
- path.toNamespacedPath(require.resolve('../fixtures/packages/missing-main/package.json')) +
- "' of 'doesnotexist.js'. Please either fix that or report it to the" +
- ' module author',
- 'DEP0128');
-assert.strictEqual(require('../fixtures/packages/missing-main').ok, 'ok');
+// A package.json with an invalid "main" entry must not fall back to index.js.
+assert.throws(
+ () => require('../fixtures/packages/missing-main'),
+ {
+ code: 'MODULE_NOT_FOUND',
+ message: /packages[/\\]missing-main[/\\]doesnotexist\.js'\. Please.+package\.json.+valid "main"/,
+ path: /fixtures[/\\]packages[/\\]missing-main[/\\]package\.json/,
+ requestPath: /^\.\.[/\\]fixtures[/\\]packages[/\\]missing-main$/,
+ }
+);
assert.throws(
() => require('../fixtures/packages/missing-main-no-index'),
@@ -299,7 +301,6 @@ assert.throws(
'fixtures/packages/index/index.js': {},
'fixtures/packages/main/package-main-module.js': {},
'fixtures/packages/main-index/package-main-module/index.js': {},
- 'fixtures/packages/missing-main/index.js': {},
'fixtures/cycles/root.js': {
'fixtures/cycles/folder/foo.js': {},
},