NPM is an essential skill for every front-end engineer. Getting started with the front-end, let’s get started with NPM.
What NPM
NPM is a package management tool installed with NodeJS. It is commonly used in the following scenarios:
-
Allow users to download third-party packages written by others from NPM servers for local use;
-
Allows users to download and install command line programs written by others from the NPM server for local use.
-
Allow users to upload their own packages or command-line programs to the NPM server for others to use;
2. Install NPM
So, first install NodeJs, download address open NodeJs’s official website, https://nodejs.org/en/download/ will find there are two versions: LTS (Long Term Support) and Current. Most users are advised to use LTS or Current version if they want to experience the latest features. The NPM version included with each version also has hints.
A Homebrew installation is available on Mac.
What is Homebrew? Can be interpreted as the equivalent of Yum on Red Hat, Ubuntu apt-get. But Homebrew isn’t apple’s, it’s a third party, https://brew.sh/index_zh-cn.html
1. Install Homebrew.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"Copy the code
2. Install node
brew install nodeCopy the code
3. Check whether the Node is successfully installed
node -vCopy the code
4. After node is installed, NPM is also installed. Check the NPM version
npm -vCopy the code
NPM command
Type NPM to see the commands supported by NPM
lufeis-MacBook-Pro:bin lufei$ npmUsage: npm <command>where <command> is one of: access, adduser, bin, bugs, c, cache, completion, config, ddp, dedupe, deprecate, dist-tag, docs, doctor, edit, explore, get, help, help-search, i, init, install, install-test, it, link, list, ln, login, logout, ls, outdated, owner, pack, ping, prefix, profile, prune, publish, rb, rebuild, repo, restart, root, run, run-script, s, se, search, set, shrinkwrap, star, stars, start, stop, t, team, test, token, tst, un, uninstall, unpublish, unstar, up, update, v, version, view, whoaminpm <command> -h quick help on <command>npm -l display full usage infonpm help <term> search for help on <term>npm help npm involved overviewSpecify configs in the ini-formatted file: /Users/lufei/.npmrcor on the command line via: NPM <command> --key valueConfig info can be viewed via: NPM help [email protected] /usr/local/lib/node_modules/ NPMCopy the code
The following describes the common commands: 1. Installation commands. The following commands are used to install WEEx-Toolkit: -g indicates global installation
npm install -g weex-toolkitCopy the code
On Linux or Mac, the default global installation path is nodemodules in the user’s home directory, and non-global installation path is nodemodules in the path where commands are run.
In fact, after the module is installed, there are actually two copies saved locally, one in ~/.npm and one in the nodemodules directory after decompressing the code. run
npm install
NPM is only checked for nodemodules, not ~/. NPM, that is, if a module has zip packages under ~/. NPM but is not installed in the node_modules directory, NPM will still download a new zip package from the remote repository once.
2, Install and write package.json,
npm install <name> --saveCopy the code
While installing, write the information to package.json. If package.json is present in the project path, you can install all dependencies according to the dependencies configuration using the NPM install method, so that when submitting to Github, you don’t need to submit node_modules.
3. Update the module
npm update <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.
4. Mandatory installation
npm install <name> -fCopy the code
If you want NPM to force a module to be reinstalled regardless of whether it has been installed, you can use the -f or -force arguments
5. Uninstall the module
npm uninstall <name>Copy the code
6. Search module
npm search expressCopy the code
Module warehouse
1. How does NPM Install know where to get modules?
The NPM configuration contains the repository address from which all installments will look for modules. The default warehouse address is https://registry.npmjs.org/ can manage NPM warehouse by NRM. First, install the NRM,
npm install -g nrmCopy the code
Use NRM ls to view all warehouses
lufeis-MacBook-Pro:~ lufei$ nrm ls npm ---- https://registry.npmjs.org/ cnpm --- http://r.cnpmjs.org/ taobao - https://registry.npm.taobao.org/ nj ----- https://registry.nodejitsu.com/ rednpm - http://registry.mirror.cqupt.edu.cn/ npmMirror https://skimdb.npmjs.com/registry/ edunpm - http://registry.enpmjs.org/* showjoy http://npm.showjoy.net/Copy the code
Use to select which repository is currently used
$ nrm use taobaoCopy the code
Maintaining an Image Source
NRM del <registry> ## deletenrm add <registry> <url> [home] ### addnrm test <name> ##Copy the code
2. How does NPM Update know the latest version of each module?
The NPM module repository provides a query service called Registry. In npmjs.org, for example, the query service web site is https://registry.npmjs.org/.
This url is followed by the module name, and you get a JSON object containing information about all versions of the module. Visit https://registry.npmjs.org/weex-toolkit, for example, you will see weex – toolkit modules all versions of the information.
The module name of registry url can also be followed by the version number to query information about a specific version. Such as https://registry.npmjs.org/weex-toolkit/0.6.1 returns a JSON object, dist nodes below the tarball attribute, was the zip version of the website
"dist": {"shasum": "7d0413b9a761c374f9b0c4a2413e8661921d8408","tarball": "http://registry.npmjs.org/weex-toolkit/-/weex-toolkit-0.6.1.tgz"},Copy the code
Use taobao NPM image
We all know that the domestic direct use of NPM official mirror is very slow, here recommended to use Taobao NPM mirror. Details: https://npm.taobao.org/
Taobao NPM image is a complete nPMjs.org image, you can use this instead of the official version (read only), the synchronization frequency is currently 10 minutes to ensure as much as possible with the official service synchronization.
You can use taobao’s customized CNPM (gzip compression support) command line tool instead of the default NPM:
npm install -g cnpm --registry=https://registry.npm.taobao.orgCopy the code
Install the module
cnpm install [name]Copy the code
Create a module
Node.js modules are packages of code published to NPM. First create the directory for Module
mkdir demo && cd demoCopy the code
Then create package.json, where the command line prompts you step by step to enter information about the module, where the module name and version number are mandatory
npm initCopy the code
Package.json property description
Name – Package name.
Version – Package version number.
Description – The description of the package.
Homepage – The official url of the package.
Author – The author name of the package.
Ficolin-names of other contributors to packs.
Dependencies – List of dependent packages If the dependency package is not installed, NPM automatically installs the dependency package in the node_module directory.
Repository – The type of place where package code is stored. It can be git or SVN. Git is available on Github.
Main — The main field specifies the program’s main entry file, which requires (‘moduleName’) loads. The default value for this field is index.js under the module root.
Keywords – Keywords
Then write the entry’s index.js as an example:
exports.test = function () { console.log("This is a test"); };Copy the code
To register an account at www.npmjs.org, this account will be added to the NPM local configuration for publishing the Module.
-
Officially registered https://www.npmjs.com/signup
-
There is also the command NPM adduser to register users as prompted
Then login
npm loginCopy the code
Publish, publish the entire directory, do not want to publish content modules, can be ignored by.gitignore or.npmignore file. When released, warehouse to set back to http://registry.npmjs.org
npm publishCopy the code
After the second change, update the content and version, and run
NPM version < version >Copy the code
References to packages after installation, example
var test = require('toniqian-test-module'); test.test();Copy the code
Thanks to all authors of reference articles.
Reference links:
Introduction to the NPM installation mechanism
Introduction to NPM
Nodejs Common NPM command
A step-by-step guide to creating your first NPM package