Introduction to the

NPM is the module manager for Node and is extremely powerful. It is mainly divided into three parts:

  1. Web developers can find a variety of packages on the site to use.

  2. The Registry is a large database that holds information about each package. We want to query the react package information, for example, can visit https://registry.npmjs.org/react, you will see the react module all versions of the information. The module name can also be followed by the version number or label to query information about a specific version, for example: https://registry.npmjs.org/react/16.8.1 to view the react 16.8.1 version information Specific usage is to keep up with the module name behind https://registry.npmjs.org/, you’ll get a JSON object, Contains information about all versions of the module.

  3. Command-line tool (CLI) developers can use this command line tool to interact with NPM, including package installation, distribution, etc.

The installation mechanism

The initial installation of a Node module can be divided into four simple steps:

  1. performnpm install <package>The command
  2. npmregistryThe registry queries the url of the module compression package
  3. Download the zip package and store it in the cache directory, default is~/.npm
  4. Unzip the zip package to the current projectnode_modulesdirectory

So what is the zip package url? When we query the information of the module through the registry, the returned JSON object contains a dist. Tarball property, which is the url of the compressed package of the module version.

Several ways to install packages

  • NPM I React: Installs the React module by default. Latest Latest version on the tag

  • NPM I [email protected]: Install the React module 16.8.1 version

  • NPM I react@next: Install the latest version of the React module on the next tag

  • Tarball NPM url https://registry.npmjs.org/react/-/react-16.8.1.tgz: I installed the react module 16.8.1 version

  • Tarball file NPM I file: xxxx.xxx. TGZ Tarball file can be obtained using the NPM pack command

  • git url

    npm i git+https://github.com/facebook/react.git

  • username/project

    npm i github:facebook/react

The version number

NPM uses the Semver specification as a dependent version management scheme. The version format is generally: major version number. Second version. Revision number.

  • Major Version Number (major) : The general change is very big, not compatible with the lower version.
  • Secondary Version Number (minor) : Compatible with the same large version of the API and usage.
  • Revision number (patch) : Usually used to fix bugs.
  • Sometimes the revision number may be followed by an earlier version number, for example1.0.0 - alpha. 11.0.0 - beta. 42.0.0 - rc. 1And so on. The common prior version isalphabetarcstablecspAnd so on.

release

  • Changing the Version number

    npm version major: The major version number increases by 1, and other versions return to 0.

    npm version minor: If the second version number is added by 1, the revision number returns to 0.

    npm version patch: Revision number plus 1.

    NPM Version Indicates the version number: Sets the version number to the specified version number

    npm version prerelease: The previous version number is increased by 1

    npm version prerelease --preid=<prerelease-id>: Specifies the name of an earlier version
    // Assume the current version is 1.1.1
    npm version major  / / 2.0.0
    npm version minor  / / 1.2.0
    npm version patch  / / 1.1.2
    npm version prerelease / / 1.1.2-0
    npm version prerelease --preid=alpha / / 1.1.2 - alpha. 0
    npm version 4.12.  / / 4.1.2
    Copy the code

    performnpm versionAfter the version number is changed, it is executed by defaultgit add -> git commit -> git tagOperation,commitInformation and fortagAll are version numbers.

  • Modify thecommitinformation

    If we need to modify the submission information, we only need tonpm versionAdd to command-mThe options are ok,%sWill be replaced with the version number.npm version prerelease -m "update %s"
  • Disable version submission and tag

    npm version prerelease --no-git-tag-version
  • release

    npm publish: Publishes the NPM package

tag

A tag in NPM is similar to a branch in Git. Publishers can use a tag to install it. Users can use latest tag to install it. This is very useful for our daily development. There are many times when we want to release a version for validation, but we don’t want to affect the people who are using it. We can use tags and prior versions to ship packages.

npm publish --tag alpha  // Send the version to the tag named alpha
npm i <package>@<tag>    // Install packages from the specified tag
Copy the code

link

In our daily development, we often have A situation like this: There are two projects A and B respectively. For project A, we have encapsulated some basic logic for other projects to use. Project B is our business project and depends on project A. Now A new requirement comes, which needs to change two projects AB. At this time, we have finished writing project A and want to verify whether the logic of project A is correct in project B. Of course, we can issue the version of project A and then upgrade the version of project B for verification, but the problem is that it may need to issue the version frequently. Another solution is Link.

Specific practices are as follows:

  1. Implemented in project Anpm linkCommand.
  2. Implemented in project BNPM Link A package nameCommand.

After the above two steps are done, the dependencies of A in node_modules in the B project point to our A project. So you don’t have to send version for verification, very convenient. Of course, the essence of link is soft connection.

npx

NPX Implements the Node software package. Node_modules /. Bin and the environment variable $PATH check whether the command exists. If yes, execute it. If the package does not exist, perform temporary installation, and then execute the command. After the command is executed, delete the package.

configuration

Using the NPM config command, you can manage the NPM configuration

  • npm config set <key> <value>: Set some configuration
  • npm config get <key>: Gets the specified configuration
  • npm config delete <key>: Deletes the specified configuration
  • npm config list: Configuration list
  • npm config edit: Open the configuration file with an editor

For example, we often set NPM registry of NPM config set registry at https://registry.npm.taobao.org/

npm ci

  • This command can only install the entire project at once and cannot add individual dependencies
  • The project must havepackage-lock.jsonfile
  • This is cleared before each installationnode_modules
  • Don’t rewritepackage.jsonpackage-lock.jsonfile
  • Installation is faster and more rigorous

Json and package-lock.json files will be changed when we clone a project and install NPM I. This sometimes brings some risks, and NPM CI is a good choice.

Querying Package Information

  • NPM view package name: Displays package details
  • NPM view Package name Versions: Displays all historical versions of the package
  • NPM repo package name: Opens the package source repository page
  • NPM docs package name: Document address to open the package

other

  • npm loginLanding: NPM
  • npm whoami: Displays the NPM user name
  • npm bin: Displays the path of the bin folder of NPM
  • npm root: Displays the NPM root directory

Refer to the article

  • NPM Chinese document
  • NPX use tutorial
  • Introduction to the NPM installation mechanism
  • Dumb blog