Node.js

Install dependencies globally and locally using packagejson

27 September 2026 · 10 min read

Install dependencies globally and locally using packagejson

Managing dependencies is a critical aspect of modern JavaScript development. Using package.json, you can effectively install dependencies globally and locally, ensuring your projects have the necessary libraries and tools to function correctly. Understanding the difference between global and local installations, and how to manage them through package.json, is crucial for maintaining consistent and reproducible builds. This article will guide you through the process, highlighting best practices and common pitfalls to avoid. We’ll explore the nuances of specifying dependencies, using different installation flags, and understanding the implications of each approach, empowering you to manage your project’s dependencies with confidence. It’s about creating a reliable and reproducible development environment, and using package.json is the key to doing so efficiently.

Understanding Global vs. Local Dependency Installation

When working with Node.js projects, you’ll encounter two primary ways to install dependencies: globally and locally. Local dependencies are specific to a single project and are stored within the node_modules directory in your project’s root. They are managed through the package.json file, which tracks the versions of each dependency. This isolation prevents conflicts between different projects that might require different versions of the same library. Think of it like having separate toolboxes for different projects; each toolbox contains the exact tools needed for that specific job, without interfering with the tools in other boxes.

Global dependencies, on the other hand, are installed in a system-wide directory, making them accessible to all Node.js projects on your machine. These are typically command-line tools or utilities that you want to use across multiple projects, such as the Vue CLI or create-react-app. Installing globally can be convenient, but it also introduces the risk of version conflicts if different projects require incompatible versions of a globally installed package. Therefore, use global installations sparingly and only for tools that are truly intended for global use. Using a version manager like nvm (Node Version Manager) can mitigate these issues by allowing you to switch between different Node.js versions, each with its own set of globally installed packages. According to a Stack Overflow Developer Survey, “managing dependencies” is one of the biggest challenges developers face. Source: Stack Overflow Developer Survey 2023

Choosing the right installation method depends on the type of dependency and its intended use. Libraries that are core to your project’s functionality should always be installed locally and managed through package.json. Tools that are used across multiple projects and don’t directly contribute to your project’s code can be installed globally. However, always be mindful of potential version conflicts and consider using a version manager to isolate your global installations.

Leveraging package.json for Dependency Management

The package.json file is the heart of your Node.js project’s dependency management. It’s a JSON file that contains metadata about your project, including its name, version, description, and, most importantly, its dependencies. This file allows you to specify which packages your project needs and which versions are compatible. When you run npm install (or yarn install), npm reads the package.json file and installs the specified dependencies into the node_modules directory. This ensures that everyone working on the project has the same set of dependencies, regardless of their local environment.

The package.json file typically contains two main sections for dependencies: dependencies and devDependencies. The dependencies section lists the packages that are required for your application to run in production. These are the libraries that your code directly depends on, such as React, Express, or Lodash. The devDependencies section, on the other hand, lists the packages that are only needed for development, such as testing frameworks (Jest, Mocha), linters (ESLint), and build tools (Webpack, Parcel). Separating these two types of dependencies ensures that your production builds are as lean as possible, without including unnecessary development tools. Managing these dependencies is crucial to keep your app running and avoiding breaking changes. Learn more about package management.

To add a dependency to your package.json file, you can use the npm install –save command (or yarn add ). This will install the package and automatically add it to the dependencies section of your package.json file. For development dependencies, use the –save-dev flag (or -D for short). This will add the package to the devDependencies section. Specifying version ranges using semantic versioning (semver) allows you to control which versions of your dependencies are installed, ensuring compatibility and preventing unexpected breaking changes. This is often the difference between a successful build and a failed one.

Installing Dependencies Locally: A Step-by-Step Guide

Installing dependencies locally is the recommended approach for most project dependencies. This ensures that your project has its own isolated set of packages, preventing conflicts and ensuring consistent behavior across different environments. Here’s a step-by-step guide on how to install dependencies locally using package.json:

  1. Initialize your project: If you don’t already have a package.json file, create one by running npm init -y in your project’s root directory. This will create a basic package.json file with default values.
  2. Install dependencies: Use the npm install –save command to install a dependency and add it to the dependencies section of your package.json file. For example, to install the lodash library, run npm install lodash –save.
  3. Install development dependencies: Use the npm install –save-dev command to install a development dependency and add it to the devDependencies section of your package.json file. For example, to install the eslint linter, run npm install eslint –save-dev.
  4. Verify your package.json file: Open your package.json file and verify that the installed dependencies are listed in the appropriate sections with the correct version ranges.
  5. Install all dependencies: When you clone a project or need to install all dependencies, simply run npm install in the project’s root directory. This will read the package.json file and install all the specified dependencies into the node_modules directory.

By following these steps, you can effectively manage your project’s local dependencies and ensure that everyone working on the project has the same set of packages. Remember to commit your package.json and package-lock.json (or yarn.lock) files to your version control system to track changes to your dependencies over time. These lock files are crucial for ensuring reproducible builds, as they specify the exact versions of each dependency that were installed.

When and How to Install Dependencies Globally

Global installations should be reserved for command-line tools or utilities that you want to use across multiple projects. Examples include tools like the Angular CLI, Vue CLI, or npm itself. Installing these tools globally allows you to access them from any directory on your system, making them convenient for everyday use.

To install a dependency globally, use the npm install -g command (or yarn global add ). This will install the package in a system-wide directory, typically located in /usr/local/lib/node_modules on Unix-like systems or in C:\Users\\AppData\Roaming\npm\node_modules on Windows. After installing a package globally, you should be able to run it from the command line by typing its name. However, it’s important to note that global installations can sometimes lead to version conflicts, especially if different projects require incompatible versions of the same package. The key is to be mindful of what you install globally and why.

Featured Snippet Optimization: It’s crucial to understand when to use global vs. local installations. Globally installing dependencies with npm install -g makes tools accessible system-wide, ideal for CLIs. Local installations, using npm install , isolate dependencies to a project, preventing version conflicts and ensuring consistent builds. Choosing the right method depends on the tool’s purpose and scope, with local installations preferred for project-specific libraries and global for widely used utilities.

Best Practices for Dependency Management

Effective dependency management is crucial for maintaining a stable and reproducible development environment. Here are some best practices to follow:

  • Use semantic versioning (semver): Specify version ranges in your package.json file to control which versions of your dependencies are installed. This allows you to receive bug fixes and minor updates while avoiding breaking changes.

  • Use lock files: Commit your package-lock.json (or yarn.lock) file to your version control system to ensure that everyone working on the project has the exact same versions of dependencies.

  • Keep dependencies up to date: Regularly update your dependencies to receive bug fixes, security patches, and new features. Use tools like npm outdated or yarn outdated to identify outdated dependencies.

  • Remove unused dependencies: Periodically review your package.json file and remove any dependencies that are no longer used by your project. This helps to reduce the size of your node_modules directory and improve build times.

By following these best practices, you can ensure that your project’s dependencies are well-managed, up-to-date, and free from conflicts. This will lead to a more stable, reliable, and maintainable codebase. Also, consider using tools like Renovate Bot or Dependabot to automate dependency updates and keep your project secure. Properly managing dependencies is a long-term investment in the health of your project. Node.js Documentation on Managing Dependencies.

Infographic here
FAQ: Dependency Management --------------------------
What's the difference between dependencies and devDependencies?
`dependencies` are required for the application to run in production, while `devDependencies` are only needed for development tasks like testing or building.
How do I update my dependencies?
Use `npm update` (or `yarn upgrade`) to update dependencies to the latest versions within the ranges specified in your `package.json` file.
Why should I use a lock file?
Lock files (`package-lock.json` or `yarn.lock`) ensure that everyone on the team uses the exact same versions of dependencies, preventing inconsistencies and build issues.
When should I install a package globally?
Install packages globally only when they are command-line tools or utilities that you want to use across multiple projects, such as the Vue CLI or create-react-app.
How can I check for outdated dependencies?
Run `npm outdated` or `yarn outdated` to see a list of dependencies that have newer versions available.
Effective dependency management is more than just installing packages; it's about creating a stable, predictable, and reproducible development environment. By understanding the nuances of global vs. local installations, leveraging the power of package.json, and following best practices, you can ensure that your projects are well-maintained and free from dependency-related issues. Mastering these concepts can drastically improve your development workflow and reduce the risk of unexpected problems down the line. Now that you've learned about effectively managing dependencies using package.json, it's time to apply these techniques to your projects. Start by reviewing your existing package.json files, identify any outdated or unused dependencies, and update them accordingly. Consider setting up automated dependency updates using tools like Dependabot to stay on top of security patches and new features. By proactively managing your dependencies, you'll ensure the long-term health and stability of your projects. Explore related topics such as semantic versioning and continuous integration/continuous deployment (CI/CD) pipelines to further enhance your development practices. **Question & Answer :** Using npm we can install the modules globally using `-g` option. How can we do this in the package.json file?

Suppose, these are my dependencies in package.json file

"dependencies": { "mongoose": "1.4.0", "node.io" : "0.3.3", "jquery" : "1.5.1", "jsdom" : "0.2.0", "cron" : "0.1.2" }

When i run npm install, I want only node.io to be installed globally, the rest others should be installed locally. Is there an option for this?

New Note: You probably don’t want or need to do this. What you probably want to do is just put those types of command dependencies for build/test etc. in the devDependencies section of your package.json. Anytime you use something from scripts in package.json your devDependencies commands (in node_modules/.bin) act as if they are in your path.

For example:

npm i --save-dev mocha # Install test runner locally npm i --save-dev babel # Install current babel locally 

Then in package.json:

// devDependencies has mocha and babel now "scripts": { "test": "mocha", "build": "babel -d lib src", "prepublish": "babel -d lib src" } 

Then at your command prompt you can run:

npm run build # finds babel npm test # finds mocha npm publish # will run babel first 

New NEW Note: For awhile now we have had npx, which allows you to run the devDependencies commands without needing to add them to your scripts section (if you want). For example:

npx webpack 

But if you really want to install globally, you can add a preinstall in the scripts section of the package.json:

"scripts": { "preinstall": "npm i -g themodule" } 

So actually my npm install executes npm install again … Which is weird but seems to work.

Note: you might have issues if you are using the most common setup for npm where global Node package installs required sudo. One option is to change your npm configuration so this isn’t necessary:

npm config set prefix ~/npm, add $HOME/npm/bin to $PATH by appending export PATH=$HOME/npm/bin:$PATH to your ~/.bashrc.

Another, probably better option is to just use nvm to manage Node and you won’t have that problem.