Skip to content

Commit ed315e6

Browse files
committed
:Tweak bespoke npm path resolution
1 parent 1502dc9 commit ed315e6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/utils/path-resolve.mts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export function findNpmDirPathSync(npmBinPath: string): string | undefined {
6161
) {
6262
thePath = libNmNpmPath
6363
}
64+
const hasSameLevelNmPath = isDirectorySync(
65+
path.join(thePath, 'node_modules'),
66+
)
67+
const hasOneBackNmPath =
68+
!hasSameLevelNmPath &&
69+
isDirectorySync(path.join(thePath, '../node_modules'))
6470
if (
6571
// npm bin paths may look like:
6672
// /usr/local/share/npm/bin/npm
@@ -72,15 +78,15 @@ export function findNpmDirPathSync(npmBinPath: string): string | undefined {
7278
// In practically all cases the npm path contains a node_modules folder:
7379
// /usr/local/share/npm/bin/npm/node_modules
7480
// C:\Program Files\nodejs\node_modules
75-
(isDirectorySync(path.join(thePath, 'node_modules')) ||
81+
(hasSameLevelNmPath ||
7682
// In some bespoke cases the node_modules folder is one level back.
77-
isDirectorySync(path.join(thePath, '../node_modules'))) &&
83+
hasOneBackNmPath) &&
7884
// Optimistically look for the default location.
7985
(path.basename(thePath) === 'npm' ||
8086
// Chocolatey installs npm bins in the same directory as node bins.
8187
(WIN32 && existsSync(path.join(thePath, 'npm.cmd'))))
8288
) {
83-
return thePath
89+
return hasOneBackNmPath ? path.dirname(thePath) : thePath
8490
}
8591
const parent = path.dirname(thePath)
8692
if (parent === thePath) {

0 commit comments

Comments
 (0)