Skip to content

2.2 NPM

1. Dependencies

Make sure the dependency is installed in the correct place When a project’s Node.js dependencies are installed, they are added to the node_modules folder in the root directory of the project. Check the package.json file to see if the missing module is in the project’s dependencies or devDependencies. If it is not, install the dependency

Terminal window
# Local installation of packages
npm install <dependency name>

Remove dependency

Terminal window
npm remove <dependency name>

1.1 Update the dependency

The issue may be caused by importing from an outdated module. To fix this, update the dependency to the latest version:

Terminal window
npm update [<pkg>...]

1.2 Delete and reinstall the dependencies

If all else fails, you can try the following:

Delete the node_modules folder:

Terminal window
rm -rf node_modules

Delete the package-lock.json file:

Terminal window
rm -f package-lock.json

Clear the Node.js cache, which may be corrupted:

Terminal window
npm cache clean --force

Reinstall the dependencies:

Terminal window
npm install

1.3 Global NPM Packages

The command npm install -g is used to install NPM packages globally on your system. When you install a package globally, it means that the package is available across all your projects and can be used from the command line anywhere on your machine.

  • Global Installation: The -g flag tells NPM to install the package globally, making it accessible system-wide.
  • Command-Line Tools: This is especially useful for installing command-line utilities, tools, or frameworks that you want to be able to use anywhere on your system, not just in a specific project directory.
Example Usage
  1. Installing a package globally:

    Terminal window
    # Global installation of packages
    npm install -g eslint

    This installs ESLint, a popular JavaScript linting tool, globally on your system. Once installed, you can run eslint from any directory without having to install it in every project individually.

  2. Installing a CLI tool globally:

    For example, to install Create React App, a tool for creating React applications, globally:

    Terminal window
    npm install -g create-react-app

    After this installation, you can use the create-react-app command to generate a new React project anywhere on your machine.

Why Use Global Installation?

  1. Command-line Tools: Many development tools and utilities (e.g., eslint, webpack, create-react-app, typescript, etc.) are typically installed globally so they can be executed from the command line without being tied to a specific project directory.

  2. Shared Tools Across Projects: If you have tools you use across multiple projects (such as build systems, testing tools, or project generators), installing them globally means you don’t have to install them again for each new project.

Where Are Globally Installed Packages Located?

  • On most systems, globally installed packages are stored in a central directory managed by NPM. The location depends on your OS and NPM configuration.

    You can check the directory with:

    Terminal window
    npm root -g

Uninstalling Global Packages

If you want to uninstall a globally installed package, you can use:

Terminal window
npm uninstall -g <package-name>

For example:

Terminal window
npm uninstall -g eslint

1.4 Dev Dependencies

1. 🧩 What are devDependencies?

What are devDependencies in package.json?

dependencies:

  • These are required to run your app in production.
  • Example: express – needed for the server to function.
"dependencies": {
"express": "^5.1.0"
}

🧪 devDependencies:

  • These are only needed during development (not in production).
  • Tools like linters, test runners, build tools, auto-reload, etc.
"devDependencies": {
"connect-livereload": "^0.6.1",
"livereload": "^0.9.3"
}

📌 You use these for development convenience but don’t need them when deploying your app.

2. Installing devDependencies

You install packages as devDependencies like this:

Terminal window
npm install nodemon --save-dev

Or shorthand:

Terminal window
npm i -D nodemon

3. Workflow

✅ Understand How npm install Works

When you run:

Terminal window
npm install

👉 By default, it installs both:

  • dependencies
  • devDependencies

🏭 In Production, Skip devDependencies

On your production server or CI/CD pipeline, run:

Terminal window
npm install --only=prod

OR:

Terminal window
npm ci --only=production

✅ This installs only dependencies, skipping things like nodemon, eslint, etc.

🧪 Use NODE_ENV=development or production

Set your environment variable to signal which mode you’re in.

Example (on Linux/macOS):
Terminal window
NODE_ENV=development node app.js
NODE_ENV=production node app.js
Example (on Windows CMD):
Terminal window
set NODE_ENV=production && node app.js
Inside your code:
if (process.env.NODE_ENV === 'development') {
console.log('Running in dev mode');
}

⚙️ Tools Only Run in Dev

Set up your devDependencies (like nodemon, livereload) to only run in development scripts.

package.json
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js"
}

Use:

Terminal window
npm run dev # Development mode with devDependencies
npm start # Production mode without them
What?How?
Install dev-only packagesnpm install <pkg> --save-dev
Run dev-only toolsnpm run dev
Avoid devDependencies in prodnpm install --only=prod
Detect environment in codeprocess.env.NODE_ENV

1.5 Project as Dependency

To install your project as a dependency in another project, you have a few different options, depending on whether you’re installing it locally (for development purposes) or publishing it to a public or private package registry like npm. Here’s a breakdown of how you can install your project as a dependency.

1. Install Your Project Locally as a Dependency

Step 1: Make Sure Your Project Has a package.json File

Ensure that your project has a package.json file with a "name" and "version" field. This is required for npm to recognize your project as a valid package.

For example:

{
"name": "my-package",
"version": "1.0.0",
"main": "server.js"
}

If you want to test your project locally without publishing it to npm, you can use npm link. This creates a global symlink to your project so you can use it as a dependency in other projects.

  1. In your project directory (the project you want to install):

    Terminal window
    cd /path/to/your/project
    npm link

    This will create a global symlink to your project in your global node_modules directory.

  2. In the project where you want to install it (the project that will use the dependency):

    Terminal window
    cd /path/to/your/other/project
    npm link my-package

    This will create a symlink to your project in the node_modules directory of the other project. Now you can require or import your project like any other dependency.

Step 3: Using Your Project

Now, you can require or import your project in the code of the other project like this:

const myPackage = require('my-package');

2. Install Your Project by Publishing to npm

If you want to share your project publicly (or privately) with others via npm, you’ll need to publish it to the npm registry. Here’s how you can do that:

Step 1: Set Up an npm Account

If you don’t have an npm account, you can sign up for one here.

Step 2: Login to npm

Login to npm using the command line:

Terminal window
npm login

You’ll be prompted to enter your npm username, password, and email address.

Step 3: Publish Your Project to npm

  1. In your project directory (the one with package.json), run the following command to publish it to npm:

    Terminal window
    npm publish

    By default, this will publish your package to the public npm registry.

Step 4: Install the Published Project in Another Project

Once your project is published to npm, you can install it as a dependency in any other project like so:

Terminal window
npm install my-package

This will download and install the latest version of your project from the npm registry into the node_modules folder of your project.

Step 5: Use Your Published Package

In the project that installed your package, you can use it like this:

const myPackage = require('my-package');

3. Install Your Project from GitHub (or Another Git Repo)

If you want to install your project from a GitHub repository (or any Git-based repository), you can use a Git URL in npm install.

Step 1: Ensure Your Project is on GitHub

Make sure your project is pushed to a public (or private) GitHub repository.

Step 2: Install Your Project Using the GitHub URL

You can install it in another project by running:

Terminal window
npm install git+https://github.com/your-username/your-repo.git

If your project is private, you may need to use SSH or provide an authentication token, depending on your setup.

Step 3: Use Your Project

Once installed, you can use it in the other project as you would with any other dependency:

const myPackage = require('your-repo');

4. Install Your Project Using a Local Path

If you want to install a local copy of your project (without publishing it), you can also use a local path.

Step 1: Use npm install with the Path

For example, if the project you want to install is located at /path/to/your-project, you can run:

Terminal window
npm install /path/to/your-project

This will install the project as a dependency in the node_modules directory of your current project.

2. Reinstall node_modules

To reinstall all Node.js packages, you can delete the node_modules folder and the package-lock.json file, then run npm install again.

Here are the commands:

Terminal window
rm -rf node_modules package-lock.json
npm install

This will reinstall all packages based on the package.json file.

3. Patch Package

To patch a Node.js package using patch-package, follow these steps:

  1. Install patch-package if you haven’t already:

    Terminal window
    npm install patch-package
  2. Make changes to the files inside the node_modules of the package you want to patch.

  3. After making the changes, run:

    Terminal window
    npx patch-package <package-name>

    Replace <package-name> with the actual package name.

  4. A patch file will be created inside the patches/ directory. You should commit this file to your version control system.

  5. Ensure that postinstall script is added to your package.json to apply the patch after every install:

    "scripts": {
    "postinstall": "patch-package"
    }

This will apply your patch every time npm install is run.

3. npm vs npx

npm

  • Installs and manages packages
  • npm install express, npm start |

npx

  • Executes a package without needing to install it globally
  • npx create-react-app my-app |

3.1 🛠️ npmNode Package Manager

  • Used to install dependencies (locally or globally)
  • Runs scripts from package.json
✅ Examples
Terminal window
npm install express # Installs express into node_modules
npm install -g nodemon # Installs nodemon globally
npm run dev # Runs the "dev" script in package.json

3.2 ⚡ npxNode Package Executor

  • Temporarily downloads and runs a CLI tool/package
  • No need to install globally
  • Great for one-off tools or scripts
✅ Examples:
Terminal window
npx nodemon app.js # Runs nodemon without installing it globally
npx create-react-app my-app # Runs the React app generator
npx eslint . # Lints your code using local ESLint

3.3 🎯 When to Use What?

TaskUse
Installing a packagenpm install
Running a script from package.jsonnpm run <script>
Running a tool (like nodemon, create-react-app) without installingnpx <tool>
Global CLI tool used frequentlynpm install -g <tool> (optional if using npx)