From 3fd61ee2c0994f338783c781a4a24a7a2cc13b7b 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 198afb3a7689f2..afed0f04714af5 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, @@ -287,12 +288,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