bf-workflows

NPM (Node Package Manager)

What is NPM?

NPM (Node Package Manager) is the default package manager for Node.js. It serves two primary functions:

  1. Online Repository: NPM hosts a vast repository of open-source packages and modules for Node.js, which you can search and explore at search.npmjs.com.
  2. Command Line Utility: NPM provides a command-line tool for managing packages, handling version control, and managing dependencies for Node.js projects.

NPM Logo

Core Features

Basic NPM Commands

1. Check NPM Version

To check the installed version of NPM, use the following command:

npm --version

2. Initialize a New Project

To create a package.json file for a new Node.js project, run:

npm init

You will be prompted to enter details like the project name, version, and description. For a quicker setup with default values, use:

npm init -y

3. Install Packages

To install a package and add it to the dependencies section of package.json, use:

npm install <package-name>

To install a package as a development dependency, use:

npm install <package-name> --save-dev

4. Update Packages

To update a specific package to the latest version:

npm update <package-name>

To update all packages listed in package.json:

npm update

5. Install All Dependencies

To install all the dependencies listed in package.json, use:

npm install

6. Run Scripts

To run a script defined in the scripts section of package.json, use:

npm run <script-name>

For example, to start a development server with a script called start, you would run:

npm run start

7. Uninstall Packages

To remove a package and also remove it from package.json, use:

npm uninstall <package-name>

To uninstall a package but keep it in package.json, use:

npm uninstall <package-name> --no-save

8. View Package Information

To get detailed information about a package, including its dependencies, version, and more:

npm info <package-name>

9. Search for Packages

To search for packages in the NPM registry:

npm search <query>

10. Audit Dependencies

To check for vulnerabilities in your dependencies:

npm audit

To fix vulnerabilities:

npm audit fix

Advanced NPM Features

1. Workspaces

NPM Workspaces allow you to manage multiple packages within a single repository.

2. NPM Scripts

Scripts in package.json allow you to automate common tasks.

3. Creating and Publishing Packages

4. Global vs Local Packages

5. Configuring NPM

To view or modify your NPM configuration settings:

Common Issues and Solutions

Resources