In this tutorial, we are going to learn about how to check whether an npm package (or module) is installed globally or locally using the npm list
command.
Checking globally installed packages
To check for all globally installed packages and its dependencies, run the npm list
command followed by the -g
flag.
This above command prints the all globally installed packages in tree view.
To view the globally installed packages, without their dependencies use:
You can also check if a specific package is installed globally or not using the npm list -g
followed by package name.
Checking locally installed packages
To check for all locally installed packages and their dependencies, navigate to the project folder in your terminal and run the npm list
command.
To view the locally installed packages, without their dependencies use:
You can also check if a specific package is installed locally or not using the npm list
command followed by package name.
FAQs
Packages should be installed globally when it provides an executable command that the user can execute from the shell, and it can be reused among different projects. The global installation allows the code in the package to be used as a set of tools in the computer.
How to check if a npm package is installed globally? ›
The npm list command is used to list installed npm packages.
- -g flag indicates that you want to list globally installed packages.
- –depth=0 option specifies the depth of the dependency tree to display. Setting it to 0 ensures that only the top-level packages are listed without their dependencies.
Is NPM package local or global? ›
Packages should be installed globally when it provides an executable command that the user can execute from the shell, and it can be reused among different projects. The global installation allows the code in the package to be used as a set of tools in the computer.
How to check if npm is installed correctly? ›
To see if NPM is installed, type npm -v in Terminal. This should print the version number so you'll see something like this 1.4. 28.
How to test npm package locally without publishing? ›
With your NPM package local to your machine, you'll need a way to reference/install it in the test application. Inside of the original NPM package directory, run npm link from the command line. This command will allow us to simulate installing this NPM package without it actually being published.
How to find npm global location? ›
Location of Installed Packages
The location of globally installed packages depends on your operating system and npm configuration: Windows: Packages are installed in %APPDATA%\npm\node_modules . macOS and Linux: Packages are typically installed in /usr/local/lib/node_modules or a user-specific directory like ~/.
How to install npm package globally? ›
To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory).
Does npm install globally by default? ›
npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package> ) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo ) to be able to install globally.
What is the alternative to npm global install? ›
pnpm is a popular and efficient alternative to npm. It works by installing packages in a global content-addressable store. Your projects can access these packages in the global store via hard links. This makes pnpm more efficient than npm.
How to publish npm package globally? ›
To publish a scoped package with public visibility, use npm publish --access public .
- On the command line, navigate to the root directory of your package. cd /path/to/package.
- To publish your scoped public package to the npm registry, run: npm publish --access public.
Use npm list --global to see all globally installed packages. If you just need a clear picture of the top-level global installations (the core packages you installed directly), use npm list --global --depth=0 instead. This will help you maintain a clean and manageable development environment.
How to check list of installed packages in npm? ›
To list npm user-installed packages, you can use the npm list command in the terminal. By default, this command will show a tree-like structure of all installed packages for the current project.
How to see npm package version installed? ›
To see the installed npm packages with their version, run npm ls --depth=0 . By default, this displays what is installed locally. To see the globally installed packages, add the global argument: npm ls --depth=0 -global.
How to check if npm package is installed locally? ›
The short answer
- To list globally installed packages with npm, you can use the npm ls command combined with the -g flag (short for global):
- Alternatively, to list locally installed packages present in the node_modules folder of a project, you can navigate to your project and run the npm ls command (without the -g flag):
How to install npm package locally? ›
Installing npm packages locally
- Installing. A package can be downloaded with the command npm install <package name> . For example: ...
- Using the installed package. Once the package is in node_modules, you can use it in your code. ...
- Using the --save flag with package. json. ...
- Manually adding dependencies to package. json.
How to run npm test locally? ›
Use npm pack to test your packages locally
- First: Build your Package. Before you can use npm pack you must first build your package. ...
- Second: Locate your Build Artifacts package. json. ...
- Third: Pack your artifacts. ...
- Fourth: Point package.
What happens when you install an npm package globally? ›
Installing a package globally allows you to use the code in the package as a set of tools on your local computer.
How to remove globally installed npm packages? ›
To uninstall an unscoped global package, on the command line, use the uninstall command with the -g flag. Include the scope if the package is scoped.
How to update global npm package? ›
To update one global package, run the command “npm update -g <package_name>”. To do the same for all global packages, run “npm update -g”. To upgrade package. json dependencies to the most current version while disregarding any specified versions, run “npm install -g npm-check-updates”.
Which of the following commands will show all the modules installed globally? ›
Explanation. Executing $ npm ls -g command will show all the modules installed globally.