How to tell if npm package was installed globally or locally

后端 未结 6 1297
遇见更好的自我
遇见更好的自我 2020-12-22 17:25

I am installing grunt, node, npm, bower, and grunt-cli on windows7.

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

How can I

相关标签:
6条回答
  • 2020-12-22 17:29

    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"
    

    0 讨论(0)
  • 2020-12-22 17:34

    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

    0 讨论(0)
  • 2020-12-22 17:41
    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.

    0 讨论(0)
  • 2020-12-22 17:41

    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.

    0 讨论(0)
  • 2020-12-22 17:48

    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

    C:\data\tryout\test1>npm list -g grunt
    C:\Users\xxxxxxx\AppData\Roaming\npm
    └── grunt@0.4.5
    

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

    C:\data\tryout\test1>npm list -g grunt
    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.

    0 讨论(0)
  • 2020-12-22 17:52

    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

    0 讨论(0)
提交回复
热议问题