Skip to content

Commit 50ec0b2

Browse files
committed
Replace --location=global with --global.
--global was erroneously deprecated in node v16.15.1 and npm v8.12.0 on Windows. npm/cli#4980 npm/cli#4982
1 parent 070564b commit 50ec0b2

File tree

8 files changed

+18
-39
lines changed

8 files changed

+18
-39
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"pnpm"
2727
],
2828
"engines": {
29-
"node": ">=16"
29+
"node": "^16 < 16.15.1 || >16.15.1",
30+
"npm": ">=8.12.1"
3031
},
3132
"main": "build/src/index.js",
3233
"scripts": {

src/lib/doctor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const npm = (
4242
}
4343

4444
const npmOptions = {
45-
...(options.global ? { location: 'global' } : null),
45+
...(options.global ? { global: true } : null),
4646
...(options.prefix ? { prefix: options.prefix } : null),
4747
}
4848

src/package-managers/bun.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function spawnBun(
2424
const fullArgs = [
2525
...args,
2626
...(npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : []),
27-
...(npmOptions.location === 'global' ? ['--global'] : []),
27+
...(npmOptions.global ? ['--global'] : []),
2828
]
2929

3030
return spawn('bun', fullArgs, spawnOptions)
@@ -46,7 +46,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
4646
const stdout = await spawnBun(
4747
['pm', 'ls'],
4848
{
49-
...(options.global ? { location: 'global' } : null),
49+
...(options.global ? { global: true } : null),
5050
...(options.prefix ? { prefix: options.prefix } : null),
5151
},
5252
{

src/package-managers/npm.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,6 @@ const findNpmConfig = memoize((configPath?: string): NpmConfig | null => {
209209
// this may be partially overwritten by .npmrc config files when using --deep
210210
const npmConfig = findNpmConfig()
211211

212-
/** A promise that returns true if --global is deprecated on the system npm. Spawns "npm --version". */
213-
const isGlobalDeprecated = memoize(async () => {
214-
const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
215-
const output = await spawn(cmd, ['--version'])
216-
const npmVersion = output.trim()
217-
// --global was deprecated in npm v8.11.0.
218-
return nodeSemver.valid(npmVersion) && nodeSemver.gte(npmVersion, '8.11.0')
219-
})
220-
221-
/**
222-
* @typedef {object} CommandAndPackageName
223-
* @property {string} command
224-
* @property {string} packageName
225-
*/
226-
227212
/**
228213
* Parse JSON and throw an informative error on failure.
229214
*
@@ -505,7 +490,7 @@ export async function viewOne(
505490
}
506491

507492
/**
508-
* Spawns npm with --json. Handles different commands for Window and Linux/OSX, and automatically converts --location=global to --global on npm < 8.11.0.
493+
* Spawns npm with --json. Handles different commands for Window and Linux/OSX.
509494
*
510495
* @param args
511496
* @param [npmOptions={}]
@@ -520,17 +505,12 @@ async function spawnNpm(
520505
const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
521506
args = Array.isArray(args) ? args : [args]
522507

523-
const fullArgs = args.concat(
524-
npmOptions.location
525-
? (await isGlobalDeprecated())
526-
? `--location=${npmOptions.location}`
527-
: npmOptions.location === 'global'
528-
? '--global'
529-
: ''
530-
: [],
531-
npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : [],
508+
const fullArgs = [
509+
...args,
510+
...(npmOptions.global ? [`--global`] : []),
511+
...(npmOptions.prefix ? [`--prefix=${npmOptions.prefix}`] : []),
532512
'--json',
533-
)
513+
]
534514
return spawn(cmd, fullArgs, spawnOptions)
535515
}
536516

@@ -645,8 +625,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
645625
const result = await spawnNpm(
646626
['ls', '--depth=0'],
647627
{
648-
// spawnNpm takes the modern --location option and converts it to --global on older versions of npm
649-
...(options.global ? { location: 'global' } : null),
628+
...(options.global ? { global: true } : null),
650629
...(options.prefix ? { prefix: options.prefix } : null),
651630
},
652631
{
@@ -657,9 +636,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
657636
const dependencies = parseJson<{
658637
dependencies: Index<{ version?: Version; required?: { version: Version } }>
659638
}>(result, {
660-
command: `npm${process.platform === 'win32' ? '.cmd' : ''} ls --json${options.global ? ' --location=global' : ''}${
661-
options.prefix ? ' --prefix ' + options.prefix : ''
662-
}`,
639+
command: `npm${process.platform === 'win32' ? '.cmd' : ''} ls --json${options.global ? ' --global' : ''}`,
663640
}).dependencies
664641

665642
return keyValueBy(dependencies, (name, info) => ({

src/package-managers/pnpm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const spawnPnpm = async (
7575
const cmd = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'
7676

7777
const fullArgs = [
78-
...(npmOptions.location === 'global' ? 'global' : []),
78+
...(npmOptions.global ? [`--global`] : []),
7979
...(Array.isArray(args) ? args : [args]),
8080
...(npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : []),
8181
]

src/package-managers/yarn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async function spawnYarn(
202202
const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn'
203203

204204
const fullArgs = [
205-
...(yarnOptions.location === 'global' ? 'global' : []),
205+
...(yarnOptions.global ? 'global' : []),
206206
...(Array.isArray(args) ? args : [args]),
207207
'--depth=0',
208208
...(yarnOptions.prefix ? `--prefix=${yarnOptions.prefix}` : []),

src/types/NpmOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** Options that can be provided to npm. */
22
export interface NpmOptions {
3-
location?: string
3+
global?: boolean
44
prefix?: string
55
registry?: string
66
}

0 commit comments

Comments
 (0)