From 78c5c4c208ebfad5f24f262abc9f3c03651d0909 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Tue, 19 May 2020 21:47:31 -0700 Subject: [PATCH] module: fix check for package.json at volume root Fix package.json files at the volume root so that when they contain {"type": "module"}, they behave as documented, like such a package.json file in any other folder. Fixes: https://github.com/nodejs/node/issues/33438 PR-URL: https://github.com/nodejs/node/pull/33476 Reviewed-By: Guy Bedford Reviewed-By: Jan Krems --- lib/internal/modules/cjs/loader.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 0c34e3425567ab..35eabb55f2b2ea 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -41,6 +41,7 @@ const { SafeMap, String, StringPrototypeIndexOf, + StringPrototypeLastIndexOf, StringPrototypeMatch, StringPrototypeSlice, StringPrototypeStartsWith, @@ -286,12 +287,13 @@ function readPackageScope(checkPath) { const rootSeparatorIndex = checkPath.indexOf(path.sep); let separatorIndex; while ( - (separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex + (separatorIndex = StringPrototypeLastIndexOf(checkPath, path.sep)) >= + rootSeparatorIndex ) { checkPath = checkPath.slice(0, separatorIndex); if (checkPath.endsWith(path.sep + 'node_modules')) return false; - const pjson = readPackage(checkPath); + const pjson = readPackage(checkPath + path.sep); if (pjson) return { path: checkPath, data: pjson