新建倉庫
登錄github,新建一個npm-repo倉庫
克隆私有倉庫到本地
git clone https://github.com/***/npm-repo.git
創建Personal access tokens
點擊右上角頭像,點擊Setting -》 Developer settings -》Personal access tokens,寫上備注,勾上權限。點擊生成后一定要先復制Token,下次就不顯示Token了。
初始化npm包
Setting中查看github用戶名
npm init --scope=github用戶名
輸入一些author和description,其他默認的可以直接回車,初始化完成后項目下會多一個package.json文件.
配置
在package.json
所在目錄創建一個.npmrc
文件,並添加以下內容
registry=https://npm.pkg.github.com/GitHub用戶名
.npmrc
添加token
//npm.pkg.github.com/:_authToken=剛才生成的Token
另一種方式
package.json中添加
"publishConfig": { "registry":"https://npm.pkg.github.com/" }
然后登陸
npm login --registry=https://npm.pkg.github.com > Username: USERNAME > Password: TOKEN > Email: PUBLIC-EMAIL-ADDRESS
這樣比較麻煩,每次忘記TOKEN都要重新生成一次。
發布
npm publish
@autkevin/npm-repo@1.0.0包發布成功.
使用npm包
創建.npmrc
在項目里面創建.npmrc
和上面的.npmrc
完全一樣,可以直接復制到項目中。
另一種方法
npm login --registry=https://npm.pkg.github.com > Username: USERNAME > Password: TOKEN > Email: PUBLIC-EMAIL-ADDRESS
安裝npm
包
npm install @autkevin/npm-repo@1.0.0
出現問題
安裝提示rollbackFailedOptional: verb npm-session
需要給軟件設置代理
查看代理本地端口,端口因為代理的模式不同端口也不同。可以通過瀏覽器查看代理的端口。
設置npm代理
npm config set proxy http://127.0.0.1:10809 npm config set https-proxy http://127.0.0.1:10809
如果代理需要認證的話可以這樣來設置
npm config set proxy http://username:password@server:port npm config set https-proxy http://username:pawword@server:port
設置git代理
github沒有被屏蔽可以不設
git config --global http.proxy http://127.0.0.1:10809 git config --global https.proxy https://127.0.0.1:10809
刪除代理
如果所用網絡不需要代理,則要把npm代理和git代理去掉
去掉npm代理
npm config delete proxy npm config delete https-proxy
去掉git代理
git config --global --unset http.proxy
git config --global --unset https.proxy
參考: