Understanding difference between local and global npm install #187305
-
Select Topic AreaQuestion BodyI am learning npm and I am confused about the difference between installing a package locally and globally. When should I use npm install package name and when should I use npm install -g package name. Also does global install affect my project folder or only my system. I am new to this so a simple explanation would help. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
When you install a package normally, it’s installed locally inside your project. It goes into that project’s node_modules folder and is added to package.json. Use this for libraries your project needs to run. |
Beta Was this translation helpful? Give feedback.
-
|
Use npm install package-name (local) when the package is needed by your project’s code, because it installs inside that project’s node_modules folder and only that project uses it. Use npm install -g package-name (global) when the package is a command-line tool you want to run from anywhere on your computer. A global install does not affect your project folder, it only installs the tool system-wide. |
Beta Was this translation helpful? Give feedback.
-
|
Local ( Global ( Check with:
|
Beta Was this translation helpful? Give feedback.
When you install a package normally, it’s installed locally inside your project. It goes into that project’s node_modules folder and is added to package.json. Use this for libraries your project needs to run.
When you install a package with -g, it’s installed globally on your system. This is mainly for command-line tools you want to use anywhere (like build tools or dev utilities).
A global install does not affect your project folder and does not update your package.json. It only makes the tool available system-wide.
Simple rule:
If your project depends on it → install locally.
If it’s a CLI tool you use in the terminal → install globally.