Modular commonJS
Introduction
JS is mainly to develop the back end of the performance, commonJS is a specification, nodejs and webpack is a concrete realization of commonJS
Other modular specifications: seajs.js / require.js CMD / AMD / UMD es5
effect
The variables have file scope, do not pollute the global variable
System Module
http
fs
querystring
url
Entry
require('模块名')
require('模块名').xx 按需引用
Do not specify a path: go first system modules -> then find node_modules from the project environment | bower_components (dependent on module) -> not found
Specify the path: to find the specified path -> not found
Support any type
Export
exports.自定义属性 = 值 | any
Batch Export all properties
Can output multiple times
module.exports = 值 | any
Only output once, output many times, if desired, can be integrated into a given target output
note
commonJS is nodejs default management module does not support modular management es6, but supports all es6 + syntax
NPM
The following only a brief introduction. For in-depth understanding of NPM can refer to this article, NPM's common commands .
effect
Help you install the module (package), automatically install dependencies, package management (add, delete, update, all package items)
Installed in the global environment
- Installed into the computer system environment
- Can be used in any position to use
- The global installed usually: command-line tools, scaffolding
npm install 包名 简写(npm i 包名 -g ) 安装
npm uninstall 包名 简写(npm uninstall 包名 -g) 卸载
Installation to the project environment
Use only in the current directory, you need to run on behalf of npm
Initialize the project environment
npm init 在项目包中引导创建一个package.json 文件
Npm management initialization file package.json
package-lock.json dependent files used to cure
{
"name": "npm", //项目名称
"version": "0.0.1", //版本
"description": "test and play", //描述
"main": "index.js", //入口文件
"dependencies": { //项目依赖 上线也要用
"jquery": "^3.2.1"
},
"devDependencies": { //开发依赖 上线就不用
"animate.css": "^3.5.2"
},
"scripts": { //命令行
"test": "命令行",
},
"repository": { //仓库信息
"type": "git",
"url": "git+https://github.com/alexwa9.github.io/2017-8-28.git"
},
"keywords": [ //关键词
"test",'xx','oo'
],
"author": "wan9",
"license": "ISC", //认证
"bugs": {
"url": "https://github.com/alexwa9.github.io/2017-8-28/issues" //问题提交
},
"homepage": "https://github.com/alexwa9.github.io/2017-8-28#readme" //首页
}
Project Dependencies
Only be used in the current project, on the line need to rely on this --save
//安装
npm i 包名 --save
npm install 包名 -S
npm install 包名@x.x.x -S
//卸载
npm uninstall 包名 --save
npm uninstall 包名 -S
Development dependence
Only be used in the current project, on the line, do not need to rely on the --save-dev
npm install 包名 --save-dev
npm install 包名 -D
View package
npm list 列出所有已装包
npm outdated 版本对比(安装过得包)
npm info 包名 查看当前包概要信息
npm view 包名 versions 查看包历史版本列表
And install any dependencies
npm install
All the packages installed inside the specified package.json
Version constraint
^x.x.x 约束主版本,后续找最新
~x.x.x 保持前两位不变,后续找最新
* 安装最新版本
x.x.x 指定一个版本
Select source
npm install nrm -g 安装选择源的工具包
nrm ls 查看所有源
nrm test 测试所有源
nrm use 切换源名
Suddenly installation card
ctrl + c -> npm uninstall 包名 -> npm cache 清除缓存 -> 换4g网络 -> npm install 包名
Release package
- Official website registration
- log in
npm login
log in- Enter user / password / email
- Creating a Package
npm init -y
- Creating entrance index.js
- Write, output
- release
npm publish
- Iteration
- Revision number
npm publish
- delete
npm unpublish
Package release, iteration, delete, needs to be in the package directory
Delete packages sometimes need to send a message
Spread
peerDependencies release dependent
optionalDependencies optional dependency
bundledDependencies bundled rely
contributors to contribute to your packing people. Contributors are a group of people.
files contained in the project file. You can specify individual files or entire directories using wildcards to include files that meet specific criteria
YARN
installation
Baidu network disk installation package
link: https://pan.baidu.com/s/1UmeUhduxGLrWrQMWOv_pog
extraction code: 27ml
Note: Do not npm i yarn -g with yarn is easy to install, but should go to the official website to download the archive to ensure hard write to the registry and environment variables, global post-installation package by yarn convenience
use
Initialize a new project
yarn init
Add dependencies
yarn add [ 包名 ]
yarn add [ 包名 ]@[version]
yarn add [ 包名 ]@[tag]
Add good yarn.lock it will automatically generate a file dependencies later, similar package.json as to help us record downloaded package.
Note :
If this project uses yarn to install the package, do not back the project in other environments such as npm download package, because more or less will be some conflict.
The dependency to different dependencies category
They were added to the dependencies
, devDependencies
, peerDependencies
and optionalDependencies
categories:
yarn add [ 包名 ] --save | -S
yarn add [ 包名 ] --dev | -D
yarn add [ 包名 ] --peer
yarn add [ 包名 ] --optional
Upgrade dependencies
yarn upgrade [ 包名 ]
yarn upgrade [ 包名 ]@[version]
yarn upgrade [ 包名 ]@[tag]
Removing dependencies
yarn remove [ 包名 ]
All rely installation project
yarn
or
yarn install
Installed in the global
yarn global add [ 包名 ] //global的位置测试不能变
yarn global remove [ 包名 ]
BOWER
Installation bower
npm install -g bower
Installation package to the global environment
bower i 包名 -g 安装
bower uninstall 包名 -g 卸载
Installation package to the project environment
Initialize the project environment
bower init
bower.json third-party package management profiles
Project Dependencies
Only be used in the current project, on the line, this also needs to rely on --save
//安装
同npm
bower install 包名#x.x.x -S 指定版本使用#
//卸载
同npm
Development dependence
Only be used in the current project, on the line, do not need to rely on the --save-dev
同npm
Entry
require('模块名')
require('模块名').xx 按需引用
Do not specify a path: go first system modules -> then find node_modules from the project environment | bower_components (dependent on module) -> not found
Specify the path: to find the specified path -> not found
Support any type
Export
exports.自定义属性 = 值 | any
Batch Export all properties
Can output multiple times
module.exports = 值 | any
Output only once
note
commonJS is nodejs default management module does not support modular management es6, but supports all es6 + syntax
At home against the war, fearless epidemic, refueling Wuhan, China refueling, humans win! ! !