aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-03-20 17:00:57 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-27 17:11:53 +0100
commit115f0f5a57f50f6b039f28a56910207f92df116d (patch)
treeb8937769e8c49b2a51b4ed7772e6bafc2657a14a /lib
parent7bddfcc61a5a7d04583a8c4fec462ca5ce45b677 (diff)
downloadandroid-node-v8-115f0f5a57f50f6b039f28a56910207f92df116d.tar.gz
android-node-v8-115f0f5a57f50f6b039f28a56910207f92df116d.tar.bz2
android-node-v8-115f0f5a57f50f6b039f28a56910207f92df116d.zip
module: throw an error for invalid package.json main entries
We currently ignore invalid `main` entries in package.json files. This does not seem to be very user friendly as it's certainly an error if the `main` entry is not a valid file name. So instead of trying to resolve the file otherwise, throw an error immediately to improve the user experience. To keep it backwards compatible `index.js` files in the same directory as the `package.json` will continue to be resolved instead but that behavior is now deprecated. PR-URL: https://github.com/nodejs/node/pull/26823 Fixes: https://github.com/nodejs/node/issues/26588 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/modules/cjs/loader.js46
1 files changed, 35 insertions, 11 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index ea1faf9b7a..f205b47a4a 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -54,6 +54,7 @@ const {
ERR_REQUIRE_ESM
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
+const pendingDeprecation = getOptionValue('--pending-deprecation');
module.exports = Module;
@@ -224,15 +225,41 @@ function readPackage(requestPath) {
}
}
-function tryPackage(requestPath, exts, isMain) {
- var pkg = readPackage(requestPath);
+function tryPackage(requestPath, exts, isMain, originalPath) {
+ const pkg = readPackage(requestPath);
- if (!pkg) return false;
+ if (!pkg) {
+ return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
+ }
- var filename = path.resolve(requestPath, pkg);
- return tryFile(filename, isMain) ||
- tryExtensions(filename, exts, isMain) ||
- tryExtensions(path.resolve(filename, 'index'), exts, isMain);
+ const filename = path.resolve(requestPath, pkg);
+ let 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 = path.resolve(requestPath, 'package.json');
+ err.requestPath = originalPath;
+ // TODO(BridgeAR): Add the requireStack as well.
+ throw err;
+ } else if (pendingDeprecation) {
+ const jsonPath = path.resolve(requestPath, 'package.json');
+ process.emitWarning(
+ `Invalid 'main' field in '${jsonPath}' of '${pkg}'. ` +
+ 'Please either fix that or report it to the module author',
+ 'DeprecationWarning',
+ 'DEP0128'
+ );
+ }
+ }
+ return actual;
}
// In order to minimize unnecessary lstat() calls,
@@ -351,10 +378,7 @@ Module._findPath = function(request, paths, isMain) {
// try it with each of the extensions at "index"
if (exts === undefined)
exts = Object.keys(Module._extensions);
- filename = tryPackage(basePath, exts, isMain);
- if (!filename) {
- filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
- }
+ filename = tryPackage(basePath, exts, isMain, request);
}
if (filename) {