Modular commonJS
commnJs is a major I normative js to the back end of the performance and development of
Note: commonJS is nodejs default management module does not support modular management es6 but supports all es6 + syntax
node.js modules are divided into three types:
built-in module node module comes with its own
third-party modules packaged modules need to use someone else (npm install -g module name) to download the project
from its own custom module package module
- Custom Modules
exports. custom attribute = value of
bulk properties may output multiple outputs are
// a.js 文件内容
let aa = 100;
let bb = 200;
exports.aa = aa;
exports.bb = bb;
//b.js文件内容
let b = require("./a.js");
console.log(b) //结果如下
{ aa: 100, bb: 200 }
module.exports = value
output only once
//a.js文件内容
let a = 10
let b = "bmw"
let fn = ()=>{
console.log("函数")
}
class Person{
constructor(){
console.log("这是Person的构造函数")
}
show(){
console.log("这是show方法...")
}
}
module.exports = {
a,b,fn,Person2:Person
}
//b.js文件内容
let a1 = require("./mod/b")
console.log(a1)//结果如下
{ a: 10, b: 'bmw', fn: [Function: fn], Person2: [Function: Person] }
asl
Download npm package management tool node comes
Role: Installation and Management Pack
- Installed in the global environment
安装 npm i 包名 -g
写在 npm uninstall -g
g : global 全局的意思
- Installation to the project environment
- Initialize the project environment
方法1
npm init
需要 自己配置一些参数
方法2
npm init -y
参数自动生成 但是目录名必须是英文
Initialization completion generates two files
package.json file management pack
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"//首页
}
- Download project dependencies
can only be on the line in the current project also relies --save
//安装
npm i 包名 --save
npm install 包名 -S
npm install 包名@x.x.x -S
//卸载
npm uninstall 包名 --save
npm uninstall 包名 -S
- Download Development dependencies
In the writing project when you do not need to use the line item --save-dev
npm install 包名 --save-dev
npm install 包名 -D
- View package
npm list 列出所有已安装包
npm outdated 版本对比(安装过的包)
npm info 包名 查看当前包的概要信息
npm view 包名 versions 查看包历史版本列表
- Install all dependencies
npm install //安装package.json里面指定的所有包
- Version constraint
^x.x.x 约束主版本,后续找最新
~x.x.x 保持前两位不变,后续找最新
* 装最新
x.x.x 定死了一个版本
Select source
Select the image source installation kit npm install nrm -g
See all source nrm ls
test all sources nrm test
handover source nrm use source name
- Installation Caton solution
ctrl + c -> npm uninstall 包名 -> npm cache verify 清除缓存 -> 换4g网络 -> npm install 包名
Release package
-
In npm official website to register an account
-
log in
At the command line, type npm login sign in
and then enter a user / password / email -
Creating a Package
asl s init
Create a file entry index.js
Output prepared
for example as follows:
//sum.js文件
const sum = (a,b) => {
return a + b
}
module.exports = sum;
//index.js 文件
const sum = require("./sum")
module.exports = {
sum
}
//package.json文件
{
"name": "gengwentuo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
- release
npm publish
Note: To check the value package.json file name exists in the npm official website
- Iteration
Meaning that after you upload packet modification
at the time of publication inside the file version number should change my package.json
- delete
npm unpublish
Published iteration package needs to be deleted in the package directory
delete packages sometimes need to send a message
bower
- Global installed bower
bower i -g package name (installation)
Bower Uninstall package name -g (unloaded)
- Initialize the project environment
bower init
generated bower.json third-party package configuration file management
- Download Project Dependencies
// install
bower install the package name #xxx -S (specify the version #)
// unload
bower uninstall package name
- Download the development of dependence
bower install the package name -D
yarn
-
Recommend Quguan network download yarn
-
Initialize the project
yarn init
- Download Package
yarn add 包名
yarn add 包名@x.x.x -S //下载
yarn add 包名@x.x.x -D //
- Delete Package
yarn remove 包名
- All package installation project
yarn 或者 yarn install
- Use yarn install a global
yarn gloabal add 包名
yarn gloabal remove 包名