zoukankan      html  css  js  c++  java
  • 发布一个简单的npm包

    本文简单地记录了发布一个简单npm包的过程,以便后续参考使用。

    初始化npm init

    通过npm init创建一个package.json文件

    D:
    obinlibweapp-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:
    obinlibweapp-utilspackage.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字段是入口文件

    写好README

    一个完备的README文件是必要的,以便别人了解你的包是做什么用途。

    确认registry

    一般我们开发时会修改npm registryhttps://registry.npm.taobao.org

    但是发布npm包时,我们需要将其改回来,不然是会报错的。

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

    npm注册账号

    打开npm官网,开始注册账号。

    ps:记得要验证邮箱哦!

    添加npm账户

    使用npm adduser添加账户,别名npm login

    D:
    obinlibweapp-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添加配置项,不加也没事,看自己需求。

    "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博客

  • 相关阅读:
    教你如何开发一个完败Miracast的投屏新功能
    深度评测--安卓免费的银行卡识别SDK,华为 HMS MLKit 和Card IO哪个好?
    别让算法和数据结构拖你职业生涯的后腿
    接入卡片类快服务你必须知道的事情
    手机智眼——智慧视觉
    解放双手的革命——智慧语音
    Android | 超简单集成HMS ML Kit实现最大脸微笑抓拍
    快服务常见TOP3审核雷区,再不过审就要崩溃啦!
    图标新纪元——服务直达
    集成机器学习服务上架华为应用市场指南
  • 原文地址:https://www.cnblogs.com/wenbinjiang/p/11563303.html
Copyright © 2011-2022 走看看