node相关工具-02学习
一、NVM:Node Version Manager
1.1Mac安装nvm
https://github.com/nvm-sh/nvm/blob/master/README.md
1.2Windows安装nvm
nvm-windows
nodist
二、NPM:Node Package Manager
** 2.1全局安装package**
npm install forever --global (-g)
forever
npm uninstall forever --global
forever
2.2package.json初始化
pwd
npm init -y
ls
cat package.json
npm i --production // 只安装生产环境的包
npm list
npm i jquery@2.2.4 -S // 版本号安装
npm view jquery versions // 查看jquery 的版本号
node package versions
13.4.6
major:13,minor:4,patch:6
主版本号:major,次版本号:minor, 打补丁:patch(偶数为稳定,奇数为不稳定)
^ 只锁定主版本号
~ 锁定主版本号和次版本号
* 表示最新版本
{
"name": "demo",
"version": "1.0.0",
"description": "qiaokun",
"main": "index.js",
"scripts": {
"serve": "./node_modules/.bin/gulp -v",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"underscore": "^1.12.1"
},
"devDependencies": {
"gulp": "^4.0.2"
}
}
2.3清除缓存
npm cache clean --force
2.4自己写的包发布
注册网站:
https://www.npmjs.com/
先注册网站,一定要邮箱验证,不然发不上去包
1.npm init -y
在根目录下,新建index.js
index.js
const _ = require('lodash')
function myChunk(arr) {
let arr2 = _.chunk(arr, 2)
return arr2
}
module.exports = myChunk
package.json文件
{
"name": "qiaokun-custom",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.21"
}
}
需要安装 npm install lodash
npm config get registry // 查看当前npm 源
npm config set registry https://registry.npmjs.org // 切换npmjs源
npm config set registry http://registry.npm.taobao.org/ // 切换淘宝镜像
报500可能是没有切换源
报403可能是新注册的用户没有进行邮箱验证
1.npm adduser
输入用户名:(自己的用户名:qiaokunmmc)
输入密码:(自己的密码:**********)
输入邮箱:
2.npm publish
发布成功之后可以进入npmjs.com中查看
使用
先安装自己发布的包:
npm install qiaokun-custom
使用
const myChunk = require('qiaokun-custom')
const arr = [
{ id: 1, name: '分类一' },
{ id: 2, name: '分类二' },
{ id: 3, name: '分类三' },
{ id: 4, name: '分类四' },
{ id: 5, name: '分类五' },
{ id: 6, name: '分类六' }
]
console.log(myChunk(arr))