@@ -86,6 +86,18 @@ const readNpmConfig = () => {
86
86
87
87
const npmConfig = readNpmConfig ( )
88
88
89
+ /** A promise that returns true if --global is deprecated on the system npm. Spawns "npm --version". */
90
+ const isGlobalDeprecated = new Promise ( ( resolve , reject ) => {
91
+ const cmd = process . platform === 'win32' ? 'npm.cmd' : 'npm'
92
+ return spawn ( cmd , [ '--version' ] )
93
+ . then ( ( output : string ) => {
94
+ const npmVersion = output . trim ( )
95
+ // --global was deprecated in npm v8.11.0.
96
+ resolve ( semver . valid ( npmVersion ) && semver . gte ( npmVersion , '8.11.0' ) )
97
+ } )
98
+ . catch ( reject )
99
+ } )
100
+
89
101
/**
90
102
* @typedef {object } CommandAndPackageName
91
103
* @property {string } command
@@ -245,19 +257,29 @@ function filterPredicate(options: Options): (o: Packument) => boolean {
245
257
}
246
258
247
259
/**
248
- * Spawn npm requires a different command on Windows .
260
+ * Spawns npm. Handles different commands for Window and Linux/OSX, and automatically converts --location=global to --global on node < 8.11.0 .
249
261
*
250
262
* @param args
251
263
* @param [npmOptions={ }]
252
264
* @param [spawnOptions={ }]
253
265
* @returns
254
266
*/
255
- function spawnNpm ( args : string | string [ ] , npmOptions : NpmOptions = { } , spawnOptions : Index < any > = { } ) : Promise < any > {
267
+ async function spawnNpm (
268
+ args : string | string [ ] ,
269
+ npmOptions : NpmOptions = { } ,
270
+ spawnOptions : Index < any > = { } ,
271
+ ) : Promise < any > {
256
272
const cmd = process . platform === 'win32' ? 'npm.cmd' : 'npm'
257
273
args = Array . isArray ( args ) ? args : [ args ]
258
274
259
275
const fullArgs = args . concat (
260
- npmOptions . location ? `--location=${ npmOptions . location } ` : [ ] ,
276
+ npmOptions . location
277
+ ? ( await isGlobalDeprecated )
278
+ ? `--location=${ npmOptions . location } `
279
+ : npmOptions . location === 'global'
280
+ ? '--global'
281
+ : ''
282
+ : [ ] ,
261
283
npmOptions . prefix ? `--prefix=${ npmOptions . prefix } ` : [ ] ,
262
284
'--depth=0' ,
263
285
'--json' ,
@@ -358,6 +380,7 @@ export const list = async (options: Options = {}) => {
358
380
const result = await spawnNpm (
359
381
'ls' ,
360
382
{
383
+ // spawnNpm takes the modern --location option and converts it to --global on older versions of npm
361
384
...( options . global ? { location : 'global' } : null ) ,
362
385
...( options . prefix ? { prefix : options . prefix } : null ) ,
363
386
} ,
0 commit comments