發佈一個簡單的npm包

本文簡單地記錄了發佈一個簡單npm包的過程,以便後續參考使用。javascript

初始化npm init

經過npm init建立一個package.json文件html

D:\robin\lib\weapp-utils>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (weapp-utils)
version: (1.0.0)
description: some foundmental utils for weapp
entry point: (lib/index.js)
test command:
git repository:
keywords: weapp,utils
author: tusi666
license: (ISC) MIT
About to write to D:\robin\lib\weapp-utils\package.json:

{
  "name": "weapp-utils",
  "version": "1.0.0",
  "description": "some foundmental utils for weapp",
  "main": "lib/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "weapp",
    "utils"
  ],
  "author": "tusi666",
  "license": "MIT"
}
複製代碼

其中main字段是入口文件java

寫好README

一個完備的README文件是必要的,以便別人瞭解你的包是作什麼用途。git

確認registry

通常咱們開發時會修改npm registryhttps://registry.npm.taobao.orggithub

可是發佈npm包時,咱們須要將其改回來,否則是會報錯的。shell

npm config set registry http://registry.npmjs.org/
複製代碼

npm註冊帳號

打開npm官網,開始註冊帳號。npm

**ps:**記得要驗證郵箱哦!json

添加npm帳戶

使用npm adduser添加帳戶,別名npm login小程序

D:\robin\lib\weapp-utils>npm adduser
Username: tusi666
Password:
Email: (this IS public) cumtrobin@163.com
Logged in as tusi666 on https://registry.npm.taobao.org/.
複製代碼

添加github倉庫

package.json添加配置項,不加也沒事,看本身需求。bash

"repository": {
  "type": "git",
  "url": "https://github.com/xxx/zqh_test2.git"
}
複製代碼

發佈

npm publish
複製代碼

若是發佈時報這樣的錯,

The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
複製代碼

建議仍是檢查下registry,或者npm adduser是否是成功了。

發佈成功,會有這樣的提示,

npm notice
npm notice package: weapp-utils@1.0.0
npm notice === Tarball Contents ===
npm notice 397B   package.json
npm notice 1.1kB  LICENSE
npm notice 2.7kB  README.md
npm notice 12.9kB lib/index.js
npm notice === Tarball Details ===
npm notice name:          weapp-utils
npm notice version:       1.0.0
npm notice package size:  5.1 kB
npm notice unpacked size: 17.1 kB
npm notice shasum:        a7f2f428d9334dd1dd749d2a492dbc4df7195d0d
npm notice integrity:     sha512-Cp8jPhOMq73y6[...]bfofe7X+4cLeg==
npm notice total files:   4
npm notice
+ weapp-utils@1.0.0
複製代碼

npm搜索weapp-utils,發現有了!

npm查詢到所發佈的包

調用

發佈成功了,也要驗證下,是否可正常使用。

import { merge } from "weapp-utils"

let mergedOptions = merge(DEFAULT_OPTIONS, options)
複製代碼

首發連接


掃一掃下方小程序碼或搜索Tusi博客,即刻閱讀最新文章!

Tusi博客
相關文章
相關標籤/搜索