Hey everyone! I’m just starting out with Node.js and NPM. I’ve been using the npm install command to set up my projects, but I’m a bit confused about some of the flags.
I keep seeing people use npm install -g when installing packages. What does the -g flag actually do? I’ve tried looking it up online, but I’m still not sure I get it.
Can someone explain what -g means in simple terms? And are there other important flags I should know about when using NPM?
Thanks in advance for any help! I’m excited to learn more about how NPM works.
hey mate, the -g flag is for global installs. it puts the package on ur whole system, not just ur project. handy for tools u use everywhere, but can mess things up if ur not careful.
other flags to know: -D for dev stuff, --save-exact for exact versions. stick to local installs mostly tho, safer that way
When you include the -g flag with npm install, the package is installed globally, meaning it becomes available across the entire system rather than being confined to a single project. This is particularly useful for command-line utilities needed for various projects. However, global installations can sometimes lead to version conflicts between projects, so it is usually preferable to install packages locally, keeping each project self-contained. In addition, there are options for adding packages as development dependencies or ensuring an exact version is used, details of which can be found in the npm documentation.
I’ve been using npm for a few years now, and I remember being confused about the flags at first too. The -g flag stands for ‘global’. When you use it, you’re installing the package globally on your system instead of just in your project folder.
This is useful for tools you want to use across multiple projects, like command-line utilities. For example, I use npm install -g nodemon to install Nodemon globally so I can use it in any project without having to install it every time.
Other flags I find helpful are -D (or --save-dev) for dev dependencies and -E (or --save-exact) to lock the version. Just be careful with global installs—they can sometimes cause version conflicts between projects. I usually prefer local installs unless I really need a tool everywhere.
Hope this helps! Let me know if you have any other questions about npm flags.