Introduction to the
- NPM is software developed based on Node.js
- NPM is the default package management tool for Node.js and an essential tool for modern front-end development
Version information
Since the nodeJS installation package already has the NPM package manager built-in, in other words, the nodeJS installation has already installed the NPM successfully, but sometimes, we need to check whether the NPM is successfully installed on a computer, or check the version information of the NPM on a device, using the following command:
npm -v
Copy the code
NPM mirror
Due to theNPM default image
It is in a foreign country, so the download speed is very slow and unstable, so we need to switch back to the domestic mirror (the most famous mirror in China is Ali).
View the current NPM image
npm config get registry
Copy the code
Set up the mirror
npm config set registry https://registry.npm.taobao.org
Copy the code
NRM Image Management Tool (recommended)
/ / install NPM NRM - g - registry=https://registry.npm.taobao.org / / I see all mirror image and the current use of NRM ls/switch/mirror NRM use taobaoCopy the code
How do I update NPM
// NPM is a normal packageCopy the code
The basic use
The installation package
NPM I XXX -s NPM I [email protected] -s NPM I XXX -d NPM I XXX -d NPM I XXX -s NPM I XXX -d NPM I XXX -s NPM I XXX -dCopy the code
Uninstall packages
// Uninstall global NPM uninstall XXX -g // uninstall development NPM uninstall XXX -d // uninstall production NPM uninstall XXX -sCopy the code
Update package
NPM update -g NPM update -g XXX NPM update -g XXX NPM update -g XXX 1. It is updated according to the version rules of the package.json fileCopy the code
Check the package
// View a global package NPM ls -g XXX // View a local package NPM ls XXX // View the package NPM ls that the current project depends onCopy the code
How is the NPM package used in a project
The NPM installation packages are stored in the node_moudules folder
Introducing way
// Node.js uses the CommonJS module specification to introduce const $= require('jquery'); // In webpack, you can use CommonJS or ES6 modularity specification import $from 'jquery'; const $ = require('jquery'); Note: WebPack is so powerful that CommonJS modules can also be imported using the ES6 specification, and conversely, ES6 modules can also be imported using the CommonJS specificationCopy the code
The introduction of the path
NPM installed packages: just import the package name (package.json name) non-NPM installed packages (custom modules) : the current file is the relative path,./ is the current folder,.. / is the upper-level folderCopy the code
The introduction of the suffix
NPM installed packages: no suffix required unless otherwise specified non-NPM installed packages (custom modules) : js can be omitted, but is recommendedCopy the code
How do I run the NPM package command on the cli
Some NPM packages provide package commands (stored in.node_modules/.bin) that can be executed on a terminal (command line tool) to use the NPM package functionality.
NPM packages (e.g., Babel) provide several ways to run them:
1. The CLI
NPM package comes with commands, you can execute in the command line, or through NPM script script. If NPM is 5.2.0 or later, you can also directly use NPX to execute commands. In actual development, this method is rarely used, because it is too cumbersome, and you have to enter commands in the command line every time you change
2. Integration mode
Integrated into application build packaging tools such as WebPack or rollup, this method is recommended for convenience and simplicity
Release NPM package
Create and initialize the package project
1. Create a directory. 2Copy the code
Ignore files
As with git code repositories, you must use files that you want to ignore when distributing packages
There are several methods:
1. Use.gitignore to set which files to ignore
Ignore files set to.gitignore are ignored in git code versioning and NPM publish
2. Use.npmignore to set which files to ignore
.npmignore is written exactly like the.gitignore rule. If both. Npmignore and. Gitignore are used, only. Npmignore takes effect and has a higher priority
3. Package. jsonfiles
Field selects which files to publish
The files field in package.json sets which files or directories to publish. This priority is higher than.npmignore and.gitignore
NPM publish default ignore rule
*.swp._ *.ds_store.git.hg.npmrc.lock-wscript.svn. wafpickle-* config.gypi CVS NPM -debug Node_modules // will be included by default and will not work even if it is ignoredCopy the code
Publish a package
Publish --access public NPM publish --access publicCopy the code
The above command will cause the package to be published to the NPM repository and given the latest flag (the latest version is available, the default NPM I package name is the latest version installed).
Update package
An updated version
NPM provides NPM version for version control. The effect is the same as manually changing the version field in package.json. The advantage is that the NPM version command can be used automatically during build process. And it has Semantic versioning.
npm version [<newversion> | major | minor | patch | premajor | preminor |
prepatch | prerelease | from-git]
Copy the code
Its semantics are:
Major: major version (major version) Minor: minor version (minor update) Patch: patch number (patch) PreMajor: pre-major version PreMinor: pre-patch version prerelease: pre-release versionCopy the code
For example, if the initial version is 1.0.0, the corresponding semantics are as follows:
NPM Version Patch // 1.0.1 indicates minor bug fixes. NPM Version Minor // 1.1.0 indicates minor new features. NPM Version mMajor // 2.0.0 indicates major version or major upgrade NPM Version Preminor // 1.1.0-0 is followed by 0, indicating a pre-releaseCopy the code
You can see the corresponding version change in the package.json of the current module
updates
We may also need to run tests after updating, so we must publish the latest to the NPM repository and have someone install the tests, but do not affect the current latest stable version, so we cannot directly run the following command:
// Publish the update package with the default flag latest NPM publishCopy the code
We need to tag the update package (which needs to be tested) so that it is not the default latest tag, using the following command:
// To install the beta, execute the NPM I package name @betaCopy the code
After the update package test passes, change the mark of the version to latest and run the following command
NPM dist-tag add Package name @ Version latestCopy the code
Unused tags (beta) can be removed using the following command
NPM dist-tag Rm package name betaCopy the code
Cancel the package
Because the undo conference does not allow packages that depend on the package to be undone to work properly, the NPM’s official revoking of packages is limited:
- It is not allowed to undo packages that have been published for more than 24 hours (
unpublish is only allowed with versions published in the last 24 hours
) - If you do want to undo within 24 hours, add the –force parameter
- Even if the released package is revoked, it cannot be re-released with the same name/version as the previously revoked package, because the uniqueness of the two has been occupied and is not officially deleted with the cancellation
npm unpublish
The unpublish command is NPM unpublish
NPM unpublish my-test-project NPM ERR! Refusing to delete entire project. npm ERR! Run with --force to do this. npm ERR! NPM unpublish my-test-project --force NPM WARN using <@scope>/]< PKG >[@<version> --force I sure hope you know what you are doing. - my-test-projectCopy the code
NPM Deprecate (recommended)
Recommended alternative to NPM unpublish:
npm deprecate <pkg>[@<version>] <message>
Copy the code
This command does not undo existing packages on NPM, but will give deprecated warnings when anyone tries to install the package, such as:
npm deprecate my-test-project 'this package is no longer maintained'
Copy the code