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

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