diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index fc333e937d6b..958ec93f089d 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2942,6 +2942,9 @@ npx codemod @nodejs/timers-deprecations -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 01a5343e92dd..a0ce91d9f7fe 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 f9c4a62f023a..6eac47c2e1a5 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 3b43284aec76..000000000000 --- 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 1e2efc6fd26a..40e3a2eb45d2 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': {}, },