Skip to content

An Introduction to the npm Package Manager

npm (Node Package Manager) is the default package manager for Node.js. It helps developers manage libraries, tools, and dependencies for their projects. Let’s explore how npm works and how to use it effectively.

Installing npm

npm is automatically installed when you install Node.js. To check if npm is installed, run the following command in your terminal:

Check npm Version
npm -v

This command will display the installed version of npm.

Key npm Commands

Here are some basic npm commands you’ll use frequently:

  1. Initialize a Project:
    To create a new Node.js project, navigate to your project directory and run:

    Initialize Project
    npm init

    Follow the prompts to set up your package.json file, which stores information about your project and its dependencies.

  2. Install a Package:
    To install a package, use the npm install command. For example, to install the popular Express.js framework, run:

    Install a Package
    npm install express

    This command adds Express.js to your project and updates the package.json and node_modules folder.

  3. Install a Package Globally:
    Some packages are meant to be used as command-line tools. To install a package globally, use the -g flag:

    Install Globally
    npm install -g nodemon

    This allows you to use the package across different projects.

  4. Installing All Dependencies: If you have a package.json file with a list of dependencies, run this command to install all of them:

    Install all Dependencies
    npm install
  5. Updating Packages:
    To update a package to the latest version, use:

    Update a Package
    npm update package-name
  6. Uninstall a Package:
    To remove a package from your project, use:

    Uninstall a Package
    npm uninstall package-name