Skip to content

2.2 NPM

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>

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>...]

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

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.
  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.

  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?

Section titled “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

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

What are devDependencies in package.json?

  • These are required to run your app in production.
  • Example: express – needed for the server to function.
"dependencies": {
"express": "^5.1.0"
}
  • 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.

You install packages as devDependencies like this:

Terminal window
npm install nodemon --save-dev

Or shorthand:

Terminal window
npm i -D nodemon

When you run:

Terminal window
npm install

👉 By default, it installs both:

  • dependencies
  • 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

Section titled “🧪 Use NODE_ENV=development or production”

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

Terminal window
NODE_ENV=development node app.js
NODE_ENV=production node app.js
Terminal window
set NODE_ENV=production && node app.js
if (process.env.NODE_ENV === 'development') {
console.log('Running in dev mode');
}

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

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

Section titled “1. Install Your Project Locally as a Dependency”

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

Section titled “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"
}
Section titled “Step 2: Link Your Project Locally (Without Publishing)”

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.

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

Section titled “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:

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

Login to npm using the command line:

Terminal window
npm login

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

  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

Section titled “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.

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)

Section titled “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.

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

Step 2: Install Your Project Using the GitHub URL

Section titled “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.

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

Section titled “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.

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.

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.

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.

========================

Great question! You’re absolutely right to notice that npx can run packages without installing them globally — but here’s the key point:


✅ Why You Still Need to Install patch-package Locally

Section titled “✅ Why You Still Need to Install patch-package Locally”

While npx patch-package <package-name> lets you run the patch creation step, you also need patch-package to:

1. Reapply patches after every npm install

Section titled “1. Reapply patches after every npm install”

This is done by running:

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

Which runs your locally installed patch-package (not the one invoked via npx).


2. Ensure consistent patching in CI/CD and across developers

Section titled “2. Ensure consistent patching in CI/CD and across developers”

If patch-package is in your devDependencies, it’s:

  • installed along with the project,
  • used by postinstall, and
  • guaranteed to be the same version for all collaborators and build pipelines.

You only need npx for the initial patch creation step:

Terminal window
npx patch-package some-package

This is convenient because you don’t need to install patch-package globally.

But for ongoing use (postinstall), it must be installed locally:

Terminal window
npm install patch-package --save-dev

Use Casenpx ok?Local install needed?
Create a patch✅ Yes❌ No (optional)
Apply patch after install❌ No✅ Yes (via postinstall)
Share in version control❌ No✅ Yes
CI/CD pipelines❌ No✅ Yes

Let me know if you want a minimal working setup or want to test a patch step-by-step.

================ TROUBLESHOOTING

The error you’re seeing:

Error: Can't find yarn.lock file

Yes — patch-package does require a lockfile to work correctly. That means:

  • If you’re using npm, it expects a package-lock.json
  • If you’re using Yarn, it expects a yarn.lock

patch-package needs to know exactly which version of each dependency is installed, and where it’s located in node_modules. Lockfiles provide this resolution map.

Without it, it can’t safely determine:

  • Which version you’re patching
  • Whether that patch will remain stable across installs

  1. If using npm:

    Terminal window
    npm install

    This generates:

    • node_modules/
    • package-lock.json
  2. Then you can run:

    Terminal window
    npx patch-package your-package-name --package-manager=npm
  3. Don’t forget to commit both:

    • patches/ folder
    • package-lock.json

ToolRequiresHow to generate
patch-package + npmpackage-lock.jsonnpm install
patch-package + yarnyarn.lockyarn install

Let me know if you want to try a patch together or see an example.

====================================================

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 |
  • Used to install dependencies (locally or globally)
  • Runs scripts from package.json
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
  • Temporarily downloads and runs a CLI tool/package
  • No need to install globally
  • Great for one-off tools or scripts
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
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)