Last Updated : 14 Aug, 2024
NPM is the default package manager for Node.js , and it is used to install, manage, and distribute JavaScript packages. When you add a package using NPM install, the location of the installed package depends upon whether the package is installed globally or locally.
Table of Content
- Local Installation
- Global Installation
- Common Issues and Solutions
- Conclusion
Local Installation
Local installation happens when you run npm install package-name command.
When you install a package locally, it is placed in the node_modules directory within your project folder. This means the package is available only to that specific project and not accessible globally.
Steps to Install Package Locally
To install a package locally, you typically run:
npm install <package-name>
npm i <package-name> #shorthand
For example, to install the express package locally, you would run:
npm install express
Location of Locally Installed Packages
The packages are installed in the node_modules directory within your project folder, and the dependencies are listed in the package.json file . For instance, if your project structure is:
/my-project
|-- package.json
|-- node_modules/
| |-- express/
The express package will be installed in /my-project/node_modules/express.
Benefits of Local Installation
- Project-Specific : Dependencies are specific to the project, avoiding conflicts with other projects.
- Easier Version Control : You can easily manage different versions of packages for different projects.
- Portable : The entire project, including its dependencies, can be easily shared or moved.
Global Installation
The global installation takes place with the help of global attribute with npm install.
When you install a package globally, it is placed in a system-wide directory and made available from any location on your system. This is useful for installing tools and utilities that you want to use across multiple projects.
Step to Install Package Globally
To install a package globally, use the -g or –global flag:
npm install -g <package-name>
For example, to install the nodemon package globally, you would run:
npm install -g express
To List all the Global Packages in the system
npm list -g --depth 0
Output:
Location of Globally Installed Packages
The location of globally installed packages depends on your operating system and npm configuration:
- Windows : In windows, packages are installed in %APPDATA%\npm\node_modules .
- macOS and Linux : In mac or Linux packages are typically installed in /usr/local/lib/node_modules or a user-specific directory like ~/.npm-global .
You can find the exact location by running:
npm root -g
Benefits of Global Installation
- Accessible Everywhere : Packages are available system-wide and can be used in any project.
- Convenient for CLI Tools : Ideal for installing command-line tools that you use frequently.
Common Issues and Solutions
- Permission Issues : On Unix-like systems, you might encounter permission issues when installing globally. You can resolve this by configuring a custom directory for global installations as mentioned above.
- Version Conflicts : Different projects might require different versions of a package. Use local installations to isolate dependencies.
- Path Not Found : If globally installed packages are not found in the path, ensure your system’s PATH variable includes the global npm directory.
Conclusion
NPM installs packages locally in the node_modules directory within your project or globally in a system-wide directory (/usr/local/lib on macOS/Linux or %AppData%\npm on Windows) when using the -g flag. By mastering these installation methods, you can manage your Node.js projects more efficiently, avoid conflicts, and streamline your development workflow.
Improve
Please Login to comment...
FAQs
NPM installs packages locally in the node_modules directory within your project or globally in a system-wide directory (/usr/local/lib on macOS/Linux or %AppData%\npm on Windows) when using the -g flag.
Where do npm install files go? ›
On most systems, this is /usr/local . On Windows, it's %AppData%\npm . On Unix systems, it's one level up, since node is typically installed at {prefix}/bin/node rather than {prefix}/node.exe . When the global flag is set, npm installs things into this prefix.
How to find installed packages in npm? ›
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.
Where are npm packages hosted? ›
There are two standard places for storing npm packages: https://registry.npmjs.org and https://github.com. You must have access only to them. Install some packages and create npm-shrinkwrap. json file (using command npm shrinkwrap for npm<5, in npm>5 this file is called package-lock.
Should npm packages be installed globally or locally? ›
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 find the path of npm? ›
locate-path
- Install. $ npm install locate-path.
- Usage. Here we find the first file that exists on disk, in array order. ...
- API. locatePath(paths, options?) ...
- Related. path-exists - Check if a path exists.
What files does npm install? ›
By default, npm install will install all modules listed as dependencies in package. json . With the --production flag (or when the NODE_ENV environment variable is set to production ), npm will not install modules listed in devDependencies .
How to find globally installed npm packages? ›
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. If you want to see a list of globally installed packages (user-installed), you can add the -g flag.
How do I know if npm is installed? ›
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 install specific npm packages? ›
To install a specific version of an npm package, you can use the npm install command along with the package name and the desired version number. Here's the syntax: Copied! Replace <package_name> with the name of the package and <version_number> with the specific version you want to install.
npm-check checks for outdated, incorrect, and unused dependencies. To use npm-check from the command line you have to install it. It requires Node >= 0.11. After installing it, it can be used with typing npm-check in the root project directory, where the package.
Where does npm run from? ›
Scripts are run from the root of the package folder, regardless of what the current working directory is when npm run is called.
Where are npm config files located? ›
global configuration file (defaults to $PREFIX/etc/npmrc ; configurable via CLI option --globalconfig or environment variable $NPM_CONFIG_GLOBALCONFIG )
Where are npm packages cached? ›
The npm cache system
On Unix-like operating systems, the cache folder is located in the ~/. npm directory, and on Windows, in the %LocalAppData%\npm-cache directory.
Where do I run npm install from? ›
You should run it in your project root folder, or the folder above your node_modules folder as sometimes the structure can differentiate between projects. But in general: the root folder of your project, as long as it is one folder above your node_modules.
How to locally install npm packages? ›
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.
Where does Node look for modules? ›
If the path starts, / Node. js will look for the module from the root of our file system. If the path begins with ./ of ../ Node. js will search for the module relative to the current file's location.