Common Commands for npm

A collection of npm commands, as well as commands commonly used for Next theme upgrades.

This post was translated from my Chinese blog post with the aid of ChatGpt.

npm is the default package manager for the JavaScript runtime environment Node.js.

Commands

Reference: jm365

Help

  • npm -l View usage information for all commands.
  • npm <command> -h View usage information for a specific command.
  • npm help <command> View detailed usage information for a specific command.
  • npm -v View the installed version of npm.
  • npm config list -l View the configuration settings for npm.
  • npm ls View all locally installed modules.
  • npm ls -g View all globally installed modules.
  • npm ls <moduleName> View the local installation status of a specific module.
  • npm ls <moduleName> -g View the global installation status of a specific module.
  • npm view <moduleName> View information about a specific module in the current registry.
  • npm view <moduleName> versions View all historical versions of a specific module in the current registry.
  • npm view <moduleName> version View the latest version of a specific module in the current registry.

Creating a Project

  • npm init Initialize a project in the current directory.

Installation/Uninstallation

  • npm i/install <moduleName> Install a module; both i and install have the same function.
  • npm i <moduleName>@x.x.x Install a specific version of a module.
  • npm i <moduleName> --save Install and save to the dependencies in the package.json file.
  • npm i <moduleName> --save-dev Install and save to the devDependencies in the package.json file.
  • npm i <moduleName> -g Globally install a module.

Uninstallation

  • npm uninstall <moduleName> Uninstall a specified local module.
  • npm uninstall <moduleName> -g Uninstall a specified global module.

Updating

  • npm update Update modules according to the descriptions in package.json, and save the updated version descriptions to package.json. ^a.b.c updates to the latest version under a, ~a.b.c updates to the latest version under a.b, and a.b.c won’t be updated.
  • npm update <moduleName> Update a specified local module.
  • npm update <moduleName>@x.x.x Update a specified local module to a specific version.
  • npm update <moduleName> -g Update a specified global module.
  • npm update <moduleName>@x.x.x -g Update a specified global module to a specific version.

npm Registry

  • npm config get registry View the current npm registry address.
  • npm config set registry <registryAddress> Set the npm registry to the specified address.

Introduction to package.json

The package.json file is the manifest of your project, where npm and yarn store the names and versions of all installed packages. The npm install command uses it to download the required modules. Below are explanations of fields in package.json.

  • version indicates the current version.
  • name sets the name of the application/package.
  • description provides a short description of the application/package.
  • main specifies the entry point of the application.
  • private if set to true, prevents accidental publishing of the application/package to npm.
  • scripts defines a set of runnable Node.js scripts.
  • dependencies lists npm packages installed as dependencies.
  • devDependencies lists npm packages installed as development dependencies.
  • engines specifies the versions of Node.js this package/application runs on.
  • browserslist indicates which browsers (and versions) to support.

Common Commands for Theme Upgrades

List installed packages:

1
npm ls

Check for packages that need an update:

1
npm-check

Upgrade a theme to the latest version:

1
npm install hexo-theme-next@latest

Or:

1
npm update hexo-theme-next