上傳套件到 NPM 倉庫

上傳到 NPM 需要先註冊 NPM 帳號


然後建立 Nodejs 的專案
mkdir mason-demo
cd demo
npm init
確認 package.json
{
"name": "mason-demo", # 你自己的專案名稱,不可以和現有NPM專案名稱相同
"version": "0.0.1", # 版本。每次修改都要往上增加,沒辦法覆蓋或降級。
"main": "index.js", # 進入點
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mason", # 作者
"license": "ISC", # License
"description": "" # 說明
}
並且從 Terminal 登入 NPM
npm login 會提供一個網址給你,透過瀏覽器登入,並完成 OTP 驗證後,在 Terminal 及完成登入
❯ npm login
npm notice Log in on https://registry.npmjs.org/
Login at:
https://www.npmjs.com/login?next=/login/cli/319ce977-c308-44cb-913e-02d0c58d877f
Press ENTER to open in the browser...
上傳你的專案到 NPM 倉庫
在寫好 Code 之後,就可以執行 npm publish 及完成。
❯ npm publish
npm notice
npm notice 📦 [email protected]
npm notice Tarball Contents
npm notice 0B index.js
npm notice 232B package.json
npm notice Tarball Details
npm notice name: mason-demo
npm notice version: 1.0.1
npm notice filename: mason-demo-1.0.1.tgz
npm notice package size: 273 B
npm notice unpacked size: 232 B
npm notice shasum: a9e8716e310cdb5510c40c62fe48dc587b9d120c
npm notice integrity: sha512-KBEBi9bRhGmLX[...]Hn8pOcC55jSCA==
npm notice total files: 2
npm notice
npm notice Publishing to https://registry.npmjs.org/ with tag latest and public access
+ [email protected]
忽略不想上傳的檔案
和 git 相同,有個 .npmignore
,將要隱藏的檔案和目錄寫入到裡面,即可在上傳時忽略該檔案及目錄。
vim .npmignore
上傳新版本
更改 package.json 的 version,比前一個版本數字高,並重新執行 npm publish
及上傳新版本到 NPM 倉庫。
vim package.json
npm publish