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 -lView usage information for all commands.npm <command> -hView usage information for a specific command.npm help <command>View detailed usage information for a specific command.npm -vView the installed version of npm.npm config list -lView the configuration settings for npm.npm lsView all locally installed modules.npm ls -gView all globally installed modules.npm ls <moduleName>View the local installation status of a specific module.npm ls <moduleName> -gView the global installation status of a specific module.npm view <moduleName>View information about a specific module in the current registry.npm view <moduleName> versionsView all historical versions of a specific module in the current registry.npm view <moduleName> versionView the latest version of a specific module in the current registry.
Creating a Project
npm initInitialize a project in the current directory.
Installation/Uninstallation
npm i/install <moduleName>Install a module; bothiandinstallhave the same function.npm i <moduleName>@x.x.xInstall a specific version of a module.npm i <moduleName> --saveInstall and save to thedependenciesin thepackage.jsonfile.npm i <moduleName> --save-devInstall and save to thedevDependenciesin thepackage.jsonfile.npm i <moduleName> -gGlobally install a module.
Uninstallation
npm uninstall <moduleName>Uninstall a specified local module.npm uninstall <moduleName> -gUninstall a specified global module.
Updating
npm updateUpdate modules according to the descriptions inpackage.json, and save the updated version descriptions topackage.json.^a.b.cupdates to the latest version undera,~a.b.cupdates to the latest version undera.b, anda.b.cwon’t be updated.npm update <moduleName>Update a specified local module.npm update <moduleName>@x.x.xUpdate a specified local module to a specific version.npm update <moduleName> -gUpdate a specified global module.npm update <moduleName>@x.x.x -gUpdate a specified global module to a specific version.
npm Registry
npm config get registryView 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.
versionindicates the current version.namesets the name of the application/package.descriptionprovides a short description of the application/package.mainspecifies the entry point of the application.privateif set to true, prevents accidental publishing of the application/package to npm.scriptsdefines a set of runnable Node.js scripts.dependencieslists npm packages installed as dependencies.devDependencieslists npm packages installed as development dependencies.enginesspecifies the versions of Node.js this package/application runs on.browserslistindicates 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 |