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
[email protected] D:\小精灵工作室\前端\npm-test
+-- [email protected]
`-- [email protected]

@是版本号

7.版本控制

"devDependencies": {
    "jquery": "^3.3.1"
}

^3.3.1表示的是版本好 主版本号.此版本号.修改

1)包的管理

-aplha 版本底层结构还未确定,改动很大的版本
-beta 底层架构已经定好,会哟一些交互上的改动,此版本可以用于内测,公测
-rc 成熟的版本
-realse 稳定的版本,最终版本

2)模块的管理

  • 查看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

步骤:

  1. 需要切换源为官方的地址:nrm use npm
  2. 需要上官网注册账号,注意126等邮箱无法收到验证的邮箱,推荐使用gmail
  3. 登录:npm login
  4. 发布包: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
  1. 上官网查看自己发布的包
  2. 新建一个工程下载已发布的包

    本文转载自:https://blog.csdn.net/qq_33051685/article/details/84931218

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章