Skip to content

Commit e59db0d

Browse files
committed
fix: more lightweight npm bin discovery in windows
1 parent af3c48e commit e59db0d

File tree

7 files changed

+35
-9
lines changed

7 files changed

+35
-9
lines changed

bin/npm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ if [ $? -ne 0 ]; then
4242
no_node_dir
4343
fi
4444
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
45-
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
45+
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
46+
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
4647
if [ $? -ne 0 ]; then
4748
no_node_dir
4849
fi

bin/npm-prefix.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
// This is a single-use bin to help windows discover the proper prefix for npm without having to load all of npm first
3+
// It does not accept argv params
4+
5+
const path = require('path')
6+
const Config = require('@npmcli/config')
7+
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
8+
const config = new Config({
9+
npmPath: path.dirname(__dirname),
10+
// argv is explicitly not looked at since prefix is not something that can be changed via argv
11+
argv: [],
12+
definitions,
13+
flatten,
14+
shorthands,
15+
excludeNpmCwd: false,
16+
})
17+
18+
config.load().then(() => {
19+
console.log(config.globalPrefix)
20+
}).catch(err => {
21+
console.trace(err)
22+
process.exit(1)
23+
})

bin/npm.cmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ IF NOT EXIST "%NODE_EXE%" (
99
)
1010

1111
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
12-
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
12+
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
13+
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
1314
SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
1415
)
1516
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (

bin/npm.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ if ($nodebin -eq $null) {
1818
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1919

2020
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21-
$npmprefix=(& $nodeexe $npmclijs prefix -g)
21+
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
22+
$npmprefix=(& $nodeexe $npmprefixjs)
2223
if ($LASTEXITCODE -ne 0) {
2324
Write-Host "Could not determine Node.js install directory"
2425
exit 1

bin/npx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ if [ $? -ne 0 ]; then
4141
fi
4242
no_node_dir
4343
fi
44-
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
44+
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
4545
NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
46-
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
46+
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
4747
if [ $? -ne 0 ]; then
4848
no_node_dir
4949
fi

bin/npx.cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ IF NOT EXIST "%NODE_EXE%" (
88
SET "NODE_EXE=node"
99
)
1010

11-
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
11+
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
1212
SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
13-
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
13+
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
1414
SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
1515
)
1616
IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" (

bin/npx.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if ($nodebin -eq $null) {
1717
}
1818
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1919

20-
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21-
$npmprefix=(& $nodeexe $npmclijs prefix -g)
20+
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
21+
$npmprefix=(& $nodeexe $npmprefixjs)
2222
if ($LASTEXITCODE -ne 0) {
2323
Write-Host "Could not determine Node.js install directory"
2424
exit 1

0 commit comments

Comments
 (0)