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