npm
npm(Node package manager) 使用介绍
解决包之间的依赖关系
需安装node.js,下载后安装即可
npm -v node -v
更新npm
npm install npm@5.4.0 -g npm install npm@latest -g // -g 表示全局环境,只有这样,npm才可以在任何目录识别
修改镜像地址:
npm config set registry https://registry.npm.taobao.org npm config delete registry npm config set proxy=https://127.0.0.1:8087 npm config delete proxy npm config list -l
// 安装cnmp以后使用cnmp就是使用国内进行,推荐 npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install --registry=https:
`//registry.npm.taobao.org`配置目录
npm config set prefix "XXX\npm\global" npm config set cache "XXX\npm\cache"
Package.json
新建文件夹,建立package.json文件
{ "name": "xxx", // 名称不能大写,不能有注释 "version": "1.0", "main":"index.js", // 设置入口文件 "scripts":{},// 自定义命令 "author":"", "license":"ISC", "dependencies":{} // 依赖文件 }
npm常用操作
cd 20.2 // 进入文件夹 npm init -y // 初始化文件---》会生成package.json y表示默认参数 npm i jquery --save // 安装jquery -----》产生node_modules目录专门存放模块, // 与package-lock.json, // npm i bootstrap,vue // 包名不能乱写,查询地址:https://www.npmjs.com/ // save 表示依赖写入package.json // --save-dev 表示只有生产环境使用 nmp -g ---> cnmp -g npm --save ---> cnmp -S npm --save-dev ---> cnpm -D // html 标签src引入即可使用 // 此时删除node_modules,也可以 // 在另一个环境中,即可还原所有node_modules npm i // 当不需要时卸载即可 npm uninstall xxx // 更新 npm update jquery // 指定版本 npm install jquery@3.0.0
使用别人的包
// 自己的包入口文件index.js node index.js // 运行 // index.js 中使用其他包 // 会到所有的node_modules寻找名字为math的软件包,根据package.json 入口文件引入 let math = require('math') let res = math.sum([1,2,3])
评论已关闭