Skip to content

Using the NPM Command Line

PotatoScript edited this page Apr 6, 2025 · 1 revision

The NPM (Node Package Manager) command line interface (CLI) is an essential tool for managing your project’s dependencies and performing various tasks in your JavaScript development environment. In this guide, you’ll learn how to use the NPM CLI effectively to install, update, manage, and even automate your project tasks. This is a professional guide aimed at helping you master NPM from the basics to advanced uses.

Step 1: Open the Command Line Interface (CLI)

To use NPM commands, you first need to open the Command Line Interface (CLI), which can be:

  • Windows: Command Prompt or PowerShell
  • macOS: Terminal
  • Linux: Terminal (depending on your distribution)

Icon for CLI:

  • 🖥️ Command Prompt (Windows)
  • 💻 Terminal (macOS/Linux)

Step 2: Basic NPM Command Structure

NPM commands follow a basic syntax:

npm <command> <package-name> <options>

Where:

  • <command>: The action you want to perform (e.g., install, uninstall, update).
  • <package-name>: The name of the package you're working with (e.g., lodash).
  • <options>: Optional flags or parameters (e.g., --save, --global).

Step 3: Commonly Used NPM Commands

Let’s walk through some of the most commonly used NPM commands and how to use them effectively with plenty of examples.

1. Installing Packages

To install a package, use the npm install command.

Basic Installation (Locally):

npm install <package-name>

Example:

npm install lodash

This installs the Lodash library and saves it into your node_modules folder (local to your project).

Global Installation: If you want to install a package globally (making it accessible anywhere on your system), use the -g flag:

npm install -g <package-name>

Example:

npm install -g typescript

This installs TypeScript globally, allowing you to use it anywhere on your computer.


2. Viewing Installed Packages

To check which packages are installed in your project:

npm list

This will list all the locally installed packages in your project.

Example Output:

my-project@1.0.0 /path/to/your/project
├── lodash@4.17.21
└── express@4.17.1

Global Packages: To view globally installed packages, use:

npm list -g --depth=0

This command will show only the globally installed packages without listing their dependencies.


3. Uninstalling Packages

To remove a package from your project, use the npm uninstall command:

npm uninstall <package-name>

Example:

npm uninstall lodash

This removes Lodash from your project and updates the package.json file.

Uninstall Globally Installed Packages: To remove a globally installed package, use the -g flag:

npm uninstall -g typescript

4. Updating Packages

To update a package to its latest version, use the npm update command.

npm update <package-name>

Example:

npm update lodash

This will update Lodash to the latest version based on the version range defined in your package.json.

To update all packages in your project:

npm update

5. Installing Specific Versions

You can install a specific version of a package by specifying the version number.

npm install <package-name>@<version>

Example:

npm install lodash@4.17.19

This installs Lodash version 4.17.19.


6. Managing Dependencies with package.json

The package.json file is essential for tracking your project’s dependencies. When you install a package, NPM will add it to your package.json by default.

Saving Dependencies: By default, packages are saved in the dependencies section of your package.json.

If you want to install a package only for development purposes (i.e., not in production), use the --save-dev flag:

npm install --save-dev eslint

This installs ESLint and adds it to the devDependencies in your package.json.

Icon for dependencies:

  • 🛠️ Development Dependencies (e.g., Testing Tools)
  • 🚀 Production Dependencies (e.g., Frameworks, Libraries)

7. Creating a Package.json File

To start a new Node.js project, use the npm init command to create a package.json file. You can use the -y flag to skip the prompts and generate it automatically with default settings.

npm init

or

npm init -y

This creates a package.json file in your project directory.


8. Running Scripts with NPM

You can define custom scripts in the package.json file that can be run using NPM.

Example Script: In your package.json, you can define a script like this:

"scripts": {
  "start": "node index.js",
  "test": "jest"
}

To run the script, use:

npm run start

To run the test script:

npm run test

Note: The npm run command is used for custom scripts, but npm start is a shorthand for running the start script.


9. Checking for Security Vulnerabilities

NPM also helps you check for known security vulnerabilities in the installed packages. Run this command:

npm audit

This will show any vulnerabilities in the packages you’re using, along with recommendations for fixing them.


Step 4: NPM CLI Flags & Options

NPM provides several useful flags and options to customize the behavior of commands. Here are some common flags:

  • -g: Install packages globally (accessible anywhere on your computer).
  • --save-dev: Install a package as a development dependency (added to devDependencies).
  • --save: Automatically adds the package to your dependencies (this is the default).
  • --global: Equivalent to -g, installs globally.
  • --silent: Suppresses output from the command, making it run quietly.
  • --dry-run: Show what changes would be made without actually installing or updating anything.

Step 5: Advanced NPM Commands

1. Using npx to Run Packages

npx is a command that comes with NPM and allows you to execute binaries from NPM packages without installing them globally.

For example, to run the create-react-app tool without installing it globally, you can run:

npx create-react-app my-app

2. Managing Multiple Versions with nvm

If you need to manage multiple versions of Node.js (for example, for different projects), you can use NVM (Node Version Manager). Install NVM, and use the following commands to switch between Node.js versions:

nvm install 14
nvm use 14

Clone this wiki locally