發佈一個本身的NPM包

發佈一個本身的NPM包

用了這麼屢次別人發佈的包,今天我們本身發佈一個npm包html

首先檢查npm源

npm get registry
# 若是不是這個地址https://registry.npmjs.org  須要設置官網源
npm config set registry https://registry.npmjs.org

若是是使用第三方源請設置npm官方源 (注):第三方源只提供下載功能git

建立模塊

npm init 

# package.json 內容
{
  "name": "isnumber-lpz",
  "version": "1.0.0",
  "description": "Number\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[C\u001b[C\u001b[Cis\u001b[C\u001b[C\u001b[Number",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lanpangzhi/isNumber-lpz.git"
  },
  "author": "lanpangzhi",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/lanpangzhi/isNumber-lpz/issues"
  },
  "homepage": "https://github.com/lanpangzhi/isNumber-lpz#readme"
}

# 建立 index.js
touch index.js

function isNumber(number) {
  number = parseFloat(number)
  return number === number && typeof number === 'number' 
}

module.exports = isNumber

npm使用介紹裏面有裏面的字段,這裏就省略了,(注):包的name不要重名建議先去[https://www.npmjs.com/](https://www.npmjs.com/)搜索下github

註冊npm帳號(有帳號跳過這步)

npm adduser用於在npmjs.com註冊一個用戶。npm

npm adduser
# Username: 用戶名
# Password: 密碼
# Email: 郵箱

# 登陸
npm login

發佈包

npm publish

(注):發佈的時候版本號要和上次的不同,默認的發佈標籤是latest、前模塊是一個beta版,好比2.1.1-beta,那麼發佈的時候須要使用tag參數json

安裝並使用

npm i isnumber-lpz

const isNumber = require('isnumber-lpz')

console.log('12', isNumber(12))   true
console.log('12.2', isNumber(12.2))    true
console.log('x12.2', isNumber('x12.2'))   false
console.log('xacsa', isNumber('xacsa'))   false

## demo 地址
https://github.com/lanpangzhi/isNumber-lpzui

# 個人博客和GitHub地址
https://github.com/lanpangzhiurl

http://blog.langpz.comcode

相關文章
相關標籤/搜索