NPM ultra detailed tutorial
Introduction to the
NPM, full name node Package manger.
- NPM is
Node
Open module registration and management system, isNode.js
A standard publishing platform for packagesNode.js
Package publishing, dissemination, dependency control, web site:www.npmjs.com/ - NPM provides command-line tools that make it easy to download, install, upgrade, and delete packages, as well as to distribute and maintain packages as a developer
How is NPM used
- NPM is installed along with node.js. But it may not be the latest version
npm install npm@latest -g
Upgrade to the latest version.
Basic commands:
$NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPM: $NPMCopy the code
The use of NPM
NPM init initializes the package.json file
Used to initialize the package.json file. The user is asked a series of questions along the way, and if you don’t want to change the default configuration, press Enter.
If -f (for force), -y (for yes) is used, then skip the questioning phase and generate a new package.json file directly.
NPM set Sets environment variables
$ npm set init-author-name 'Your name'
$ npm set init-author-email 'Your email'
$ npm set init-author-url 'http://yourdomain.com'
$ npm set init-license 'MIT'
Copy the code
This command sets the default value for the package.json author name, mail, home page, and license fields when executing NPM init. This information is stored in the ~/.npmrc file in the user’s home directory, so that the user does not have to enter each item. If a project has different Settings, you can run NPM config for that project.
$ npm set save-exact true
Copy the code
The above command sets that when a module is added, package.json records the exact version of the module, not an optional version range.
npm config
$ npm config set prefix $dir
Copy the code
The above command sets the specified $dir directory as the module’s global installation directory. If you currently have write permission to this directory, sudo authorization is no longer required when running NPM install.
$ npm config set save-prefix ~
Copy the code
The above command causes NPM install –save and NPM install –save-dev to change the permitted version range from the symbol (^) to the tilde (~) when installing new modules, i.e. from allowing minor version upgrades to only allowing fixes.
$ npm config set init.author.name $name
$ npm config set init.author.email $email
Copy the code
The above command specifies the field defaults for the generated package.json file when using NPM init.
npm info
The NPM info command displays detailed information about each module
$ npm info underscore
$ npm info underscore description
$ npm info underscore homepage
$ npm info underscore version
Copy the code
npm search
The NPM search command is used to search the NPM repository. It can be followed by either a string or a regular expression
$NPM search < search term >Copy the code
npm list
The NPM list command lists all the modules installed for the current project in a tree structure, as well as the modules they depend on.
npm list
npm list -global
npm list vue
Copy the code
With the global parameter, globally installed modules are listed.
npm install
The Node module is installed using the NPM install command.
Each module can be “globally installed” or “locally installed”. “Global installation” refers to the installation of a module into the system directory, which can be called by individual projects. In general, global installation only applies to tool modules, such as ESLint and gulp. “Local installation” means downloading a module to the node_modules subdirectory of the current project, and then calling the module only from within the project directory
$sudo NPM install -g <package name> $sudo NPM install -g <package name Name > # also supports direct input lot code base address $NPM install git://github.com/package/path.git $NPM install git://github.com/package/path.git#0.1.0 # forced reinstalling $NPM install < packageName > -- force # if you want, all modules are mandatory to installation, NPM install $rm -rf node_modules $NPM installCopy the code
Install different versions
The install command always installs the latest version of a module, and if you want to install a specific version of a module, you can follow the module name with @ and the version number.
$NPM install sax@latest $NPM install [email protected] $NPM install sax@">=0.1.0 <0.2.0" $NPM install readable-stream --save --save-exact $NPM install sax --save $NPM install Node-tap --save-dev # $NPM install sax -s $NPM install node-tap -d $NPM install <module-name>@beta (latest beta) # Install the specified beta $NPM install <module-name>@1.3.1-beta.3 # NPM install Install dependencies and devDependencies $NPM install --production # or $NODE_ENV=production NPM installCopy the code
Avoiding System Permissions
By default, Npm global modules are installed in the system directory (such as /usr/local/lib/). Ordinary users do not have write permission and need to use the sudo command. This is not very convenient, we can install the global module without root permission.
First, create a new configuration file. NPMRC in the home directory, and then define the prefix variable in that file under the home directory.
prefix = /home/yourUsername/npm
Copy the code
Then create a new NPM subdirectory under the home directory
$ mkdir ~/npm
Copy the code
After that, globally installed modules are installed in this subdirectory, and NPM looks for commands in the ~/ NPM /bin directory.
Finally, add the PATH variable to the.bash_profile file (or.bashrc file).
export PATH=~/npm/bin:$PATH
Copy the code
npm update
The NPM update command updates locally installed modules
$NPM update [package name] $NPM update -global [package name]Copy the code
It queries the remote repository for the latest version and then the local version. If the local version does not exist, or the remote version is newer, it will be installed.
Using the -s or –save parameters, you can update the version number of the module in package.json at installation time.
Note that starting with NPM V2.6.1, NPM Update only updates the top-level module, not the dependencies of dependencies, which were recursively updated in previous versions. To get the effect of the old version, use the following command.
$ npm --depth 9999 update
Copy the code
npm uninstall
The NPM uninstall command uninstalls installed modules
$NPM uninstall [package name] $NPM uninstall [package name] -globalCopy the code
npm run
NPM can be used not only for module management, but also for executing scripts. The package.json file has a scripts field that can be used to specify script commands to be invoked directly by NPM.
The NPM run command automatically adds the node_modules/. Bin directory to the $PATH environment variable, so there is no need to add a PATH to the commands in the scripts field, thus avoiding the global installation of the NPM module.
NPM run, if run without any arguments, will list all script commands that can be executed in package.json.
NPM has two built-in command abbreviations, NPM test is equivalent to running NPM run test, and NPM start is equivalent to running NPM run start.
$ npm i eslint --save-dev
Copy the code
Pre – and post- scripts
NPM run provides pre- and post- hooks for each command. NPM run Lint: NPM run lint: NPM run lint: NPM run lint: NPM run lint: NPM run lint: NPM run lint: Finally, execute NPM run postlint.
Global module (available anywhere on the command line) Local module
- Why can global modules be used directly anywhere
Global module
- The bin parameter must be configured using package.json
- #! /usr/bin/env node
npm link
Temporarily place the current directory in the global directory.
When developing NPM modules, there are times when you want to try them out while you’re developing, such as when you’re debugging locally, require(‘myModule’) will automatically load the modules you’re developing locally. Node specifies that when a module is used, it needs to be installed in the global or project node_modules directory. For modules under development, the solution is to generate a symbolic link to the module’s local directory in the global node_modules directory.
NPM Link does this by automatically creating the symlink.
Imagine a scenario where you develop a module, myModule, with the directory SRC /myModule, and use this module for your own project, myProject, with the directory SRC /myProject. First, run the NPM link command in the module directory (SRC /myModule).
src/myModule$ npm link
Copy the code
The command above generates a symbolic link file in the global module directory of NPM with the module name specified in package.json.
/path/to/global/node_modules/myModule -> src/myModule
Copy the code
At this point, the myModule module can be called globally. However, if we want this module to be installed in the project, we need to perform the following steps.
Switch to the project directory, run the NPM link command again, and specify the module name.
src/myProject$ npm link myModule
Copy the code
The above command is equivalent to generating symbolic links for the local module.
src/myProject/node_modules/myModule -> /path/to/global/node_modules/myModule
Copy the code
You can then load the module in your project.
var myModule = require('myModule');
Copy the code
In this way, any changes to myModule can be directly reflected in myProject. However, there is a risk that any changes made to myModule in the myProject directory will be reflected in the module’s source code.
If your project no longer needs this module, use the NPM unlink command in the project directory to remove the symbolic link.
src/myProject$ npm unlink myModule
Copy the code
npm bin
$NPM bin./node_modules/.binCopy the code
npm adduser
$ npm adduser
Username: YOUR_USER_NAME
Password: YOUR_PASSWORD
Email: [email protected]
Copy the code
npm publish
NPM publish is used to publish the current module to nPMjs.com. Before executing, you need to apply for a user name from nPMjs.com.
$NPM adduser # login $NPM login # publish $NPM publish # if the current module is a beta version, such as 1.3.1-beta.3, then you need to publish it with the tag parameter. Publish to the specified tag. The default publish tag is latest $NPM publish --tag beta # If a private module is published, the scope parameter is required when the module is initialized. Only paying users of NPM can publish private modules. $NPM init --scope=<yourscope> # If your module is written in ES6, it is best to convert it to ES5 when publishing. First, you need to install Babel. $ npm install --save-dev babel-cli@6 babel-preset-es2015@6Copy the code
Then, write the build script inside package.json.
"scripts": {
"build": "babel source --presets babel-preset-es2015 --out-dir distribution",
"prepublish": "npm run build"
}
Copy the code
Running the above script will convert the ES6 source files in the source directory to the ES5 source files in the Distribution directory. Then, create two files.npmignore and.gitignore under the project root and write the following, respectively.
// .npmignore
source
// .gitignore
node_modules
distribution
Copy the code
npm deprecate
If you want to deprecate a version of a module, use the NPM deprecate command.
$NPM deprecate my-thing@"< 0.2.3" "Critical bug fixed in v0.2.3"Copy the code
github / npm
NPM version number management issues
- The Semver specification specifies that the version number consists of three MAJOR MINOR patches
- MAJOR may not be compatible with older versions
- MINOR added a new API, vue.Observable, that is compatible with older versions
- PATCH fixing bugs
Git tags correspond to NPM versions
npm version major minor patch
Copy the code
Git is automatically associated with git
Version Number Meaning
- 2.2.0 must be 2.2.0
- ^2.2.0 Limited to large versions, later updates as long as not more than 2
- ~2.2.0 specifies the first two versions. Later versions can be greater than 0
-
=2.0 is greater than this version
- < = 2.0
- 1.0.0-2.0.0
Pretest version
- Alpha Preview Internal beta
- Beta Public beta
- Rc final test version
scripts
- Command shortcuts can be configured for scripts (long commands can be placed in scripts)
- Executing the command puts the.bin folder in the current node_modules directory globally (so you can use it directly)
- NPM run start can be abbreviated to NPM start
npx
- NPX and script will help us run the contents of the.bin directory directly
- If the script exists in the. Bin directory, the script will be executed. If the script does not exist, the script will be downloaded and run
NPX is only a temporary solution. NPM5.2
Source switching (NPM NRM NVM)
- npm install nrm -g
- nrm ls / nrm use
The release of the package
- How to publish a package first register NPM account
- Be sure to post on the official feed
- NPM addUser Adds a user
- NPM publish Publish package
Reference: NPM Module Manager (Ruan Yifeng)