How to use the command "npm" (with examples)

How to use the command "npm" (with examples)

The npm command is a package manager for JavaScript and Node.js. It allows developers to manage Node.js projects and their module dependencies. With npm, users can interactively create a package.json file, install packages, manage dependencies, and uninstall packages.

Use case 1: Interactively create a package.json file

Code:

npm init

Motivation: Creating a package.json file is essential for any Node.js project, as it serves as a manifest for the project, documenting important metadata such as project name, version, dependencies, and more. By running npm init, developers can interactively generate a package.json file, answering a series of prompts to provide the necessary information.

Explanation: The npm init command initializes a new package.json file in the current directory. It prompts the user with a series of questions, such as project name, version, description, entry point, test command, Git repository, keywords, author, license, and more. The user’s answers are used to generate the package.json file.

Example output:

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

Press ^C at any time to quit.
package name: (my-project)
version: (1.0.0)
description: My awesome project
entry point: (index.js)
test command:
git repository:
keywords:
author: John Doe
license: (ISC)
About to write to /path/to/my-project/package.json:

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "My awesome project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "John Doe",
  "license": "ISC"
}

Is this OK? (yes)

Use case 2: Download all the packages listed as dependencies in package.json

Code:

npm install

Motivation: When working on a Node.js project, it is common to have dependencies listed in the package.json file that need to be installed. Running npm install allows developers to download and install all the required packages listed as dependencies in the package.json file.

Explanation: The npm install command installs all the packages listed as dependencies in the package.json file. It reads the package.json file, resolves the dependencies, and downloads the required packages from the npm registry. The downloaded packages are then stored in the node_modules directory.

Example output:

added 5 packages from 3 contributors and audited 6 packages in 1.235s

found 0 vulnerabilities

Use case 3: Download a specific version of a package and add it to the list of dependencies in package.json

Code:

npm install package_name@version

Motivation: Sometimes it is necessary to install a specific version of a package, especially when the project relies on a particular version of a package that is compatible with the codebase. By specifying the package name and version, developers can download and install the required version of the package and add it as a dependency in the package.json file.

Explanation: The npm install package_name@version command installs the specified version of a package and adds it to the list of dependencies in the package.json file. It first checks if the specified version is available in the npm registry. If found, it downloads and installs the specified version. If the version is not found, an error is returned.

Example output:

added 1 package from 1 contributor and audited 6 packages in 1.235s

found 0 vulnerabilities

Use case 4: Download the latest version of a package and add it to the list of dev dependencies in package.json

Code:

npm install package_name --save-dev

Motivation: In a development workflow, it is common to have dependencies that are only required during development and not in production. By using the --save-dev flag, developers can add packages as dev dependencies, ensuring they are only installed in the development environment.

Explanation: The npm install package_name --save-dev command downloads and installs the latest version of a package, adding it to the list of dev dependencies in the package.json file. Dev dependencies are specified in the devDependencies section of the package.json file and are not included when the application is deployed or used in production.

Example output:

added 1 package from 1 contributor and audited 6 packages in 1.235s

found 0 vulnerabilities

Use case 5: Download the latest version of a package and install it globally

Code:

npm install --global package_name

Motivation: Some packages provide command-line tools or utilities that need to be accessible across different projects or globally in the system. By installing a package globally, developers can make the package available in their system or accessible from the command line.

Explanation: The npm install --global package_name command installs the latest version of a package globally. The package is downloaded and installed in a global location, usually based on the user’s system configuration. The globally installed package can then be accessed and used from any project or directly from the command line.

Example output:

/usr/local/bin/package_name -> /usr/local/lib/node_modules/package_name/bin/executable
+ package_name@1.0.0
added 1 package from 1 contributor in 2.487s

Use case 6: Uninstall a package and remove it from the list of dependencies in package.json

Code:

npm uninstall package_name

Motivation: Over the course of development, packages may become obsolete or no longer needed. By uninstalling packages, developers can remove unused packages from their project and ensure a clean and lightweight codebase.

Explanation: The npm uninstall package_name command uninstalls a package from the project and removes it from the list of dependencies in the package.json file. It removes the package and its associated files from the node_modules directory, effectively deleting the package from the project.

Example output:

removed 1 package and audited 6 packages in 1.235s

found 0 vulnerabilities

Use case 7: List of locally installed dependencies

Code:

npm list

Motivation: It is often necessary to check the list of installed dependencies in a project, especially when working with a complex codebase that relies on multiple packages. By running npm list, developers can get an overview of all the locally installed dependencies and their versions.

Explanation: The npm list command outputs a tree-like structure that represents the dependency graph of the project. It shows all the locally installed packages and their dependencies. The tree structure provides a visual representation of the package hierarchy and helps identify any potential conflicts or outdated dependencies.

Example output:

my-project@1.0.0
├─ dependency1@1.2.3
└─ dependency2@0.7.1
   └─ dependency3@2.4.0

Use case 8: List top-level globally installed packages

Code:

npm list --global --depth=0

Motivation: When working with globally installed packages, it can be useful to get a concise list of the top-level packages, without the need to traverse the entire global dependency tree. By running npm list --global --depth=0, developers can quickly see all the globally installed packages in a more compact format.

Explanation: The npm list --global --depth=0 command lists the top-level globally installed packages. It provides an overview of the globally installed packages without showing the dependencies of those packages. The --depth=0 flag limits the output to the top-level packages, resulting in a more concise list.

Example output:

/usr/local/lib
├─ package1@1.0.0
└─ package2@2.1.3

Conclusion:

The npm command is a powerful tool for managing Node.js projects and their module dependencies. By utilizing the various use cases described above, developers can efficiently create and manage package.json files, install packages, and keep their projects organized and up to date. Whether it’s installing specific package versions, managing dev dependencies, or uninstalling packages, npm provides the necessary functionality for effective dependency management.

Related Posts

How to use the command "dart" (with examples)

How to use the command "dart" (with examples)

The dart create command is used to initialize a new Dart project.

Read More
TypeScript Compiler: Use Cases (with examples)

TypeScript Compiler: Use Cases (with examples)

TypeScript is becoming increasingly popular among developers due to its ability to add static types to JavaScript, enabling better code organization, bug prevention, and code navigation.

Read More
How to use the command pacman (with examples)

How to use the command pacman (with examples)

Pacman is the package manager utility for Arch Linux. It is used to install, upgrade, and manage software packages.

Read More