npm入门教程:
1.初始化项目
npm init -y / npm init -force
2.包安装
- 安装生产阶段的包:npm install jquery --save
- 安装开发阶段的包: npm install jquery --save-dev
- 只安装生产环境的包,删除包,然后输入 npm install --production,这里
- 只会把-save环境依然的包导入进来
3.包卸载
- 删除生产环境的包:npm uninstall jquery --save
- 删除开发环境的包:npm uninstall jquery -dev
- 删除所有jquery所有包:npm uninstall jquery
- 删除所有包:npm uninstall
- 删除多个包可以连续写:npm uninstall jquery bootstrap babel
4.通过package.json文件导入包
只需要package.json文件,使用npm install 命令既可
5.全局化安装
尽量少用全局化安装,出现问题,可以使用yard 来解决这问题,该问题在win7上基本
上都会出现
命令:npm install webpack -g / npm install webpack -global
6.查看已经安装列表
命令:npm list
D:\小精灵工作室\前端\npm-test>npm list
npm-test@1.0.0 D:\小精灵工作室\前端\npm-test
+-- babel@6.23.0
`-- jquery@3.3.1
@是版本号
7.版本控制
"devDependencies": {
"jquery": "^3.3.1"
}
^3.3.1表示的是版本好 主版本号.此版本号.修改
- 包的管理
-aplha 版本底层结构还未确定,改动很大的版本
-beta 底层架构已经定好,会哟一些交互上的改动,此版本可以用于内测,公测
-rc 成熟的版本
-realse 稳定的版本,最终版本
- 模块的管理
- 查看jquery的版本信 命令:npm info jquery
- 查看可以更新的版本 命令l: npm outdated jquery,如已经是最新版本,则无提示消息
"devDependencies": {
"jquery": "*3.3.1" //允许主版本更新
"jquery": "^3.3.1" //允许次版本更新
"jquery": "~3.3.1" //允许bug更新
}
- 更新指定模块版本 命令:npm update jquery ,会根据npm outdataed jquery的wanted的结果来更新,
- 更新所有版本 npm update
- 升级npm本身的版本 命令:npm install -g npm
8.源管理
-
需要先安装nrm来管理源: npm install -g nrm
-
查看可以用的源:nrm ls
D:\code\system-learning\font_page\npm_demo> 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/
-
切换源:nrm use 源的名称
PS D:\code\system-learning\font_page\npm_demo> nrm use taobao Registry has been set to: https://registry.npm.taobao.org/
-
测试源的延迟:nrm test
PS D:\code\system-learning\font_page\npm_demo> nrm test npm ---- 826ms cnpm --- 204ms * taobao - 138ms nj ----- Fetch Error rednpm - Fetch Error npmMirror 1563ms edunpm - Fetch Error
-
添加源的地址:nrm add 源名称 address
PS D:\code\system-learning\font_page\npm_demo> nrm add zz http://127.0.0.1:8080/ add registry zz success PS D:\code\system-learning\font_page\npm_demo> 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/ zz ----- http://127.0.0.1:8080/
9.发布自己的包到npm
步骤:
-
需要切换源为官方的地址:nrm use npm
-
需要上官网注册账号,注意126等邮箱无法收到验证的邮箱,推荐使用gmail
-
登录:npm login
-
发布包:npm publish
必须设置package.json的name为注册的名称,否则没有上传的权限
PS D:\code\system-learning\font_page\npm_demo> npm publish
npm ERR! publish Failed PUT 403
npm ERR! Windows_NT 10.0.15063
npm ERR! argv "D:\\install\\nodejs\\node.exe" "D:\\install\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "publish"
npm ERR! node v6.15.1
npm ERR! npm v3.10.10
npm ERR! code E403
npm ERR! You do not have permission to publish "npm-demo". Are you logged in as the correct user? : npm-demo
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! D:\code\system-learning\font_page\npm_demo\npm-debug.log
{
"name": "npm-demo", //需改改成注册的账户名称
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {}
}
需要验证带你的邮箱,否则无法发布
PS D:\code\system-learning\font_page\npm_demo> npm publish
npm ERR! publish Failed PUT 403
npm ERR! Windows_NT 10.0.15063
npm ERR! argv "D:\\install\\nodejs\\node.exe" "D:\\install\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "publish"
npm ERR! node v6.15.1
npm ERR! npm v3.10.10
npm ERR! code E403
npm ERR! you must verify your email before publishing a new package: https://www.npmjs.com/email-edit : kriszhang
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! D:\code\system-learning\font_page\npm_demo\npm-debug.log
- 上官网查看自己发布的包
- 新建一个工程下载已发布的包
10.使用sinopia搭建私有npm仓库
步骤
-
cmd:npm install -g sinopia,貌似需要python的环境
-
启动sinopia,会打印启动信息,默认的访问地址是:http://localhost:4873/
C:\Users\Kris>sinopia warn --- config file - C:\Users\Kris\AppData\Roaming\sinopia\config.yaml //配置地址 warn --- http address - http://localhost:4873/ //默认访问地址
-
添加用户:npm add userm,sinopia作为服务端,需要新开一个cmd做为客户端,需要添加一个sinopia服务端地址,把把源切换过去
PS D:\code\system-learning\font_page\npm_test> nrm add sinopia http://localhost:4873/ add registry sinopia success PS D:\code\system-learning\font_page\npm_test> 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/ zz ----- http://127.0.0.1:8080/ sinopia http://localhost:4873/ PS D:\code\system-learning\font_page\npm_test> nrm use sinopia
添加用户
npm@3.10.10 D:\install\nodejs\node_modules\npm PS D:\code\system-learning\font_page\npm_test> npm adduser Username: zhangzhen Password: Email: (this IS public) 287021229@qq.com Logged in as zhangzhen on http://localhost:4873/. PS D:\code\system-learning\font_page\npm_test>
成功添加用户之后,会在sinopia服务端目录下新增一个文件htpasswd,迁移的时候需要拷贝该用户配置
-
修改源和默认访问地址
如果配置的listen无法访问的话,sinpia会无法打开,会报错
C:\Users\Kris>sinopia fatal --- cannot open config file C:\Users\Kris\AppData\Roaming\sinopia\config.yaml: JS-YAML: can not read a block mapping entry; a multiline key may not be an implicit key at line 50, column 1: ^
-
重启sinopia既可以生效,需要需改客户端源的地址,并重新成功登录才能发布包;