上一篇文章《npm發佈包教程(一):從npm提及》中咱們介紹了npm相關的一些知識,旨在讓你們對npm有一些深刻的理解,這一篇咱們正式開始演示發佈過程。html
在正式開始演示前,咱們還須要作兩項準備工做:git
註冊地址github
重要!發佈scoped包時會用到
npm i nrm -g
nrm是npm倉庫管理的軟件,可用於npm倉庫的快速切換npm
nrm 經常使用命令:json
nrm //展現nrm可用命令 nrm ls //列出已經配置的全部倉庫 nrm test //測試全部倉庫的響應時間 nrm add <registry> <url> //新增倉庫 nrm use <registry> //切換倉庫
開始演示前作兩個簡短說明:
(1)npm官方建議規範的包至少包含:segmentfault
後續的演示都遵循此規範。babel
(2)本次僅演示我的帳戶的包發佈,包括一個unscoped包和一個scoped的包。團體帳戶下的包發佈流程和我的帳戶差異不大,在此不作展開。ide
yuyy-test-pkg測試
(1)建立工程文件夾網站
mkdir yuyy-test-pkg && cd yuyy-test-pkg
(2)建立package.json
npm init
按照提示一步步完善便可,也可以使用npm init -y
使用npm默認設置,稍後再經過編輯package.json修正。
注意:本次演示的包的入口文件是index.js,請務必確保package.json中字段main對應的值是「index.js」。
最終結果:
{ "name": "yuyy-test-pkg", "version": "1.0.0", "description": "my first npm package", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm", "packge" ], "author": "yuyy", "license": "ISC" }
(3)建立README.md
內容:
### yuyy-test-pkg This is my first npm package! It is just for learning.
(4)建立index.js
內容:
module.exports = { printMsg: function () { console.log('this message is from yuyy-test-pkg!'); } }
最終的目錄結構:
└── yuyy-test-pkg ├── README.md ├── index.js └── package.json
npm publish
可能報的錯:
(1)未登陸npm ERR!
code ENEEDAUTHnpm ERR!
need auth auth required for publishingnpm ERR!
need auth You need to authorize this machine using npm adduser
解決辦法:npm adduser
輸入:
(2)倉庫地址不對npm ERR!
code E409npm ERR!
Registry returned 409 for PUT on http://r.cnpmjs.org/-/user/or...:yuyy: conflict
緣由:經過nrm ls
命令查看我此時的倉庫地址爲cnpm,而不是npm
解決辦法:用nrm切換到npm倉庫,執行命令nrm use npm
以上問題解決後再次執行發佈命令npm publish
,發佈成功
.
有可能有延遲,不能立馬看不到。
@yuyy/babel
(1)建立工程文件夾
mkdir babel && cd babel
(2)建立package.json
npm init
按提示操做,最終結果:
{ "name": "babel", "version": "1.0.0", "description": "my scoped test package", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm", "package" ], "author": "yuyy", "license": "ISC" }
(3)建立README.md
內容:
### @yuyy/babel This is my scoped npm package! It is just for learn.
(4)建立index.js
內容:
module.exports = { printMsg: function () { console.log('this message is from @yuyy/babel!'); } }
最終的目錄結構:
└── babel ├── README.md ├── index.js └── package.json
npm publish
報錯:沒有發佈權限npm ERR!
publish Failed PUT 401npm ERR!
code E401npm ERR!
This package requires that publishers enable TFA and provide an OTP to publish. For more info, visit: https://go.npm.me/2fa-guide : babel
緣由:已經存在babel包,而我又不是babel的發佈者
解決:包名和域名差很少,先到先得,若是我非要發佈一個叫babel的包,只能給它加做用域放到個人命名空間下
npm init --scope=@yuyy -y
@符號後面的是你註冊npm帳戶時的username,若是不記得能夠經過npm whoami
查詢。
上面的命令實際上是在從新生成package.json,只是會給包增長了做用域,執行完後package.json如今的內容:
{ "name": "@yuyy/babel", "version": "1.0.0", "description": "my scoped test package", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm", "package" ], "author": "yuyy", "license": "ISC" }
惟一的變化是name字段由原來的babel變成了@yuyy/babel。
npm publish
報錯:npm ERR!
publish Failed PUT 402npm ERR!
code E402npm ERR!
You must sign up for private packages : @yuyy/babel
緣由:
npm publish
命令執行,默認是進行私有發佈,參見官網publish命令 解決:若是不想花錢,那隻能將包面向公共發佈,這也符合npm鼓勵開源的精神,這一點和GitHub建立倉庫相似。
npm publish --access public
執行結果:
值得注意的一點:咱們的項目名是babel,最終發佈的包名是@yuyy/babel,可見發佈的包名能夠和項目名不一致,包名取決於package.json中的name字段。
至此,咱們完成了npm包發佈的所有過程,一個unscoped包:yuyy-test-pkg,另外一個scoped包:@yuyy/babel,也包括過程當中可能遇到的問題。發佈完咱們本身的包以後,咱們會在下一篇文章《npm發佈包教程(三):安裝發佈包》中介紹安裝本身的包和涉及到的一些引用模塊相關的知識,以及後續文章中介紹如何對發佈過的包進行升級和廢棄等。
相關文章:
1.《npm發佈包教程(一):從npm提及》
2.《npm發佈包教程(二):發佈包》
3.《npm發佈包教程(三):安裝發佈包》
4.《npm發佈包教程(四):迭代》
5.《npm發佈包教程(五):廢棄/刪除》