“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

NPM is a package management tool installed with NodeJS. It can solve many problems with NodeJS code deployment. The common use scenarios are as follows:

  • Allow users to accessnpmThe server downloads third-party packages written by others and uses them locally.
  • Allow users to accessnpmThe server downloads and installs command-line programs written by others to use locally.
  • Allows users to upload packages or command-line programs they have writtennpmServers are for other people to use.

Common commands

NPM help <command> NPM help <commandCopy the code

NPM init initialization

Use NPM init to generate a package.json file

npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. # package name: (test) # version: (1.0.0) Git repository: # NPM search keywords: # author: Open Source License: (ISC)Copy the code

Note: You can run the NPM help package-json command to view the descriptions of specific fields

NPM config Configuration file

NPM config list -l # NPM config get init.author.name # NPM config set NPM config delete init.author.name # alias cCopy the code

NPM Search search module

The NPM search command is used to search the NPM repository

NPM search search term [-g] # alias find s seCopy the code

NPM ls View installed modules

NPM ls -g --depth 0 # alias list la llCopy the code

NPM view View module details

NPM view lodash # alias info show vCopy the code

NPM install Installs a module

NPM install [email protected] # Install NPM install lodash@">=4.17.1 <4.17.21" # Pass Install git address NPM install https://github.com/lodash/lodash # # # alias I add configuration items global dependence - global - g # production environment depend on the dependencies (the default) -- save-s # Dev dependencies --save-dev -dCopy the code

NPM Uninstall Uninstalls the module

Check module NPM ls lodashCopy the code

The NPM update module is updated

NPM update # OR NPM update lodashCopy the code

The update command only updates the package-lock-. json file, not the version number of the module package in the package.json file. To update the version number, use the nPm-check-updates tool

Ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates ncu install -g npm-check-updates Ncu -i axios # Specifies the version range. Ncu --target minor # OR NCU --target patchCopy the code

NPM Link reference module

NPM native package development and debugging

Create a link from the global module path to the project module path in another directory. # packageName is taken from the name field NPM link in the package.json of the package PackageName # Alias lnCopy the code
# Remove module reference NPM unlinkCopy the code

NPM run-script Indicates the execution script

The scripts field in the package.json file can be used to specify script commands for NPM to invoke directly

NPM run-script test # run rum URNCopy the code

Built-in predefined commands

You do not need to add run when using the built-in predefined commands

npm start
npm restart
npm test
npm stop
Copy the code

Commands are serial and parallel

NPM run script1 && NPM run script2 # NPM run script1 && NPM run script2 #Copy the code

Issue related commands

NPM adduser # NPM login # npmjs.com NPM PublishCopy the code

Other commands

NPM root: NPM root: NPM root: NPM root: NPM root: NPM root: NPM root: NPM root: NPM root Open GitHub repo NPM repo [package-name] # open GitHub repo NPM repo [package-name] # open GitHub repo NPM repo [package-name] # open GitHub repo NPM repo [package-name NPM prune # Lock the current project's dependent module version NPM shrinkwrapCopy the code

hook

There are two hooks in the NPM script: pre and POST

For example, the hooks for the build script command are prebuild and postbuild

{
  "scripts": {
    "prebuild": "echo I run before the build script",
    "build": "echo build",
    "postbuild": "echo I run after the build script"
  }
}
Copy the code

When the user executes the NPM run build, it is executed in the following order

npm run prebuild && npm run build && npm run postbuild
Copy the code

The default hooks

  • prepublish postpublish
  • preinstall postinstall
  • preuninstall postuninstall
  • preversion postversion
  • pretest posttest
  • prestop poststop
  • prestart poststart
  • prerestart postrestart

Pay attention to

  1. Custom script commands can also be addedpreposthook

    Such asdeployThere are alsopredeploypostdeployhook
  2. A doubleprepostNull and void,

    Such asprepretestandpostposttest

The relevant data

  • CLI commands | npm Docs
  • NPM module manager