220

I am installing Grunt, Node.js, npm, Bower, and grunt-cli on Windows 7.

The instructions say I should run the install commands with the -g flag for global.

How can I check if I used the -g flag when I installed? It will take a lot of time to uninstall them and reinstall.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AndraeRay
  • 2,348
  • 2
  • 12
  • 9

6 Answers6

310

Use the list command with the -g flag to see all packages that are installed globally:

npm list -g

To check if a specific package is installed globally, you can provide the name of package (grunt in this case) as seen below:

npm list -g grunt

Or you can use grep to filter on package names:

npm list -g | grep grunt

Source: https://docs.npmjs.com/cli/ls

yzorg
  • 4,224
  • 3
  • 39
  • 57
Muntaser Ahmed
  • 4,487
  • 1
  • 16
  • 17
  • 4
    For a boolean "installed or not?" check, try using `--depth`. For example: `npm list --depth 1 --global csats-mturk > /dev/null 2>&1`.[Here's a more detailed example](https://gitlab.com/snippets/3883). – Adam Monsen Mar 19 '15 at 23:20
  • 2
    @AdamMonsen , I was looking for _top level_ installation of tern. I used `npm list --depth 0 --global tern` to check if tern was installed globally as if `npm install -g tern` – wadkar Mar 16 '16 at 13:47
  • `grep` seems like a fairly reliable solution given the comments about npm exiting with an error due to unmet dependencies – myol Mar 19 '18 at 00:31
43
npm list --depth 1 --global packagename > /dev/null 2>&1

You can then check the exit status to see if it's installed or not. Thanks Adam Monsen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • 4
    thanks for `--depth 1`! that's what I was looking for. Actually I'm using now `npm list --depth 0 -g` – revelt Jul 28 '16 at 09:13
  • 1
    For some reason `$?` always returns `1` for me, even if the package IS installed or NOT installed. – Katie Oct 05 '16 at 18:41
  • 5
    WARNING: npm will always exit with an error if any of your packages have unmet dependencies, **even if the package is installed**. @Kayvar this is why npm is returning 1 for you. – Codebling Oct 10 '16 at 20:23
  • There is a warning (to [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))): *"npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead."* – Peter Mortensen Aug 05 '22 at 00:29
25

To check if a specific package is installed globally, execute:

npm list -g [package-name]

Let's take Grunt as an example. If it is installed globally, you should see something like this:

npm list -g grunt

Output:

C:\Users\xxxxxxx\AppData\Roaming\npm
└── grunt@0.4.5

If it is not installed globally, you should see something like this

npm list -g grunt

Output:

C:\Users\xxxxxxx\AppData\Roaming\npm
└── (empty)

To check if a specific package is installed locally you can execute the same commands as above, but without the -g parameter.

Source: How to check if npm package was installed globally or locally.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mvermand
  • 5,829
  • 7
  • 48
  • 74
10

You can list all global packages with the command:

npm ls -g

Or check for a specific package with:

npm ls -g [package-name] 

For example: npm ls -g @angular/cli

Mwiza
  • 7,780
  • 3
  • 46
  • 42
  • There is a warning (to [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))): *"npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead."* – Peter Mortensen Aug 05 '22 at 00:30
7

In Windows we use the following command to find out whether the package is installed or not. Please refer image for details.

npm list -g | find "create"

sample result

SHARATH P
  • 71
  • 1
  • 3
1

From your package with sindresorhus/is-installed-globally

https://github.com/sindresorhus/is-installed-globally

Usage:

const isInstalledGlobally = require('is-installed-globally');

// With `npm install your-package`
console.log(isInstalledGlobally);
//=> false

// With `npm install --global your-package`
console.log(isInstalledGlobally);
//=> true

I found this useful when I had to distribute prebuilt files with my package: How to publish a npm package with distribution files? With this package, I can check if the installation is local or global, and then use relative paths for local installations, as shown here.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985