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