搭建本身的npm包

基本操做步驟

1.npm官網註冊帳號,驗證郵箱(必須)
2.本地終端npm login登錄,注意:須要使用npm原版鏡,淘寶鏡像是不行的。git

npm config set registry http://registry.npmjs.org/

3.本地建立一個倉庫,例:npm

mkdir lnxbyu-test
cd lnxbyu-test
touch index.js
npm init

一路回車。json

4.把該作的都作了(建立gitignore、readme等):併發

touch .gitignore
touch README.md

5.index.js中設計代碼,例:ui

const title = 'hello world, merry chrismas~'

function helloWorld() {
    console.log(title)
    return true
}

module.exports = helloWorld

保存。this

6.終端登錄npm帳號,併發布:設計

npm login // 登錄npm,輸入帳號
npm publish // 發佈

7.驗證發佈結果,安裝一下試試code

npm install lnxbyu-test

新建index.js,導入lnxbyu-testget

const lnxbyu = require('lnxbyu-test')
lnxbyu() // hello world, merry chrismas~

完成。it

發佈過程當中遇到的幾個問題:

  • 1.沒有驗證郵箱:
報錯信息:
you must verify your email before publishing a new package: https://www.npmjs.com/email-edit
解決方案:進入 https://www.npmjs.com/email-edit驗證郵箱後可解決
  • 2.沒有使用官方鏡像:
報錯信息:
\[no\_perms\] Private mode enable, only admin can publish this module \[no\_perms\] Private mode enable, only admin can publish this module
解決方案:
npm config set registry http://registry.npmjs.org
設置npm鏡像源爲官方源,並從新登錄
  • 3.包名重複,沒法提交:
解決方案: 修改package.json中的name字段,而後從新publish

npm命令

npm login // 登錄npm帳戶
npm logout // 登出
npm whoami // 返回當前登錄(npm login)的用戶名

後續遇到問題再作記錄。

相關文章
相關標籤/搜索