• 怎样使用npm打包公布nodejs程序包


      作者:zhanhailiang 日期:2014-11-20

    不论基于不论什么语言开发内部项目,我们常常会在内部封装一些极具通用性的功能模块。假设我们认为该通用模块对其他团队有可取之处,全然能够将其开源出去。

    npm就是公布nodejs程序包的最优工具。

    1. 首先在npmjs.org注冊一个账号:(可省)

    2. 通过npm adduser来注冊新账号或登录老账号:

    [root@~/wade/nodejs/pv-tj]# npm adduser
    Username: billfeller
    Password: 
    Email: (this IS public) 531958936@qq.com

    3. 在本地通过npm init完毕对应的模块的开发,请注意在main文件里规范对外暴露的接口,注意。关于npm init的使用方法请參考之前的文章,这里不再具体说明:

    4. 完毕模块开发后就能够通过npm publish打包公布对应的程序包:

    [root@~/wade/nodejs/uv-tj]# npm publish
    + uv-tj@1.0.0

    关于npm publish的使用请见:

    5. 此时你应该能够在npmjs.org上查到uv-tj包:


    6. 接下来就能够像使用其他包一样使用uv-tj包了:

    [root@~/wade/nodejs]# git clone git@github.com:billfeller/uv-tj-demo.git
    Initialized empty Git repository in /root/wade/nodejs/uv-tj-demo/.git/
    remote: Counting objects: 5, done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 5 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (5/5), done.
    [root@~/wade/nodejs]# cd uv-tj-demo/
    [root@~/wade/nodejs/uv-tj-demo]# npm init
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sane defaults.
     
    See `npm help json` for definitive documentation on these fields
    and exactly what they do.
     
    Use `npm install <pkg> --save` afterwards to install a package and
    save it as a dependency in the package.json file.
     
    Press ^C at any time to quit.
    name: (uv-tj-demo) 
    version: (1.0.0) 
    description: 
    entry point: (index.js) 
    test command: 
    git repository: (https://github.com/billfeller/uv-tj-demo.git) 
    keywords: uv tj module demo
    author: billfeller
    license: (ISC) MIT
    About to write to /root/wade/nodejs/uv-tj-demo/package.json:
     
    {
      "name": "uv-tj-demo",
      "version": "1.0.0",
      "description": "uv-tj-demo ==========",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "https://github.com/billfeller/uv-tj-demo.git"
      },
      "keywords": [
        "uv",
        "tj",
        "module",
        "demo"
      ],
      "author": "billfeller",
      "license": "MIT",
      "bugs": {
        "url": "https://github.com/billfeller/uv-tj-demo/issues"
      },
      "homepage": "https://github.com/billfeller/uv-tj-demo"
    }
     
     
    Is this ok? (yes) yes
    [root@~/wade/nodejs/uv-tj-demo]# vim package.json 
    [root@~/wade/nodejs/uv-tj-demo]# npm i uv-tj --save
    uv-tj@1.0.0 node_modules/uv-tj
    ├── redis@0.12.1
    └── express@4.10.2 (utils-merge@1.0.0, merge-descriptors@0.0.2, fresh@0.2.4, cookie@0.1.2, escape-html@1.0.1, range-parser@1.0.2, cookie-signature@1.0.5, finalhandler@0.3.2, vary@1.0.0, media-typer@0.3.0, methods@1.1.0, parseurl@1.3.0, serve-static@1.7.1, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.0, qs@2.3.2, etag@1.5.0, on-finished@2.1.1, debug@2.1.0, proxy-addr@1.0.3, send@0.10.1, accepts@1.1.3, type-is@1.5.3)
    [root@~/wade/nodejs/uv-tj-demo]# vim index.js
    [root@~/wade/nodejs/uv-tj-demo]# npm start
     
    > uv-tj-demo@1.0.0 start /root/wade/nodejs/uv-tj-demo
    > node index.js

    此时通过浏览器訪问:


    完整代码请见:

    7. 也能够直接git clone https://github.com/billfeller/uv-tj-demo到本地,直接运行npm install来測试:

    [root@~/wade/test]# git clone https://github.com/billfeller/uv-tj-demo
    Initialized empty Git repository in /root/wade/test/uv-tj-demo/.git/
    remote: Counting objects: 9, done.
    remote: Compressing objects: 100% (7/7), done.
    remote: Total 9 (delta 1), reused 4 (delta 0)
    Unpacking objects: 100% (9/9), done.
    [root@~/wade/test]# cd uv-tj-demo/
    [root@~/wade/test/uv-tj-demo]# ls
    index.js  LICENSE  package.json  README.md
    [root@~/wade/test/uv-tj-demo]# npm install
    uv-tj@1.0.0 node_modules/uv-tj
    ├── redis@0.12.1
    └── express@4.10.2 (merge-descriptors@0.0.2, utils-merge@1.0.0, fresh@0.2.4, cookie@0.1.2, escape-html@1.0.1, range-parser@1.0.2, cookie-signature@1.0.5, finalhandler@0.3.2, vary@1.0.0, media-typer@0.3.0, methods@1.1.0, parseurl@1.3.0, serve-static@1.7.1, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.0, qs@2.3.2, on-finished@2.1.1, etag@1.5.0, debug@2.1.0, type-is@1.5.3, proxy-addr@1.0.3, send@0.10.1, accepts@1.1.3)
    [root@~/wade/test/uv-tj-demo]# npm start
     
    > uv-tj-demo@1.0.0 start /root/wade/test/uv-tj-demo
    > node index.js
  • 相关阅读:
    运行自动安装apk代码,报错: Original error: Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
    已安装Appium-Python-Client,但appium无法导入WebDriver
    打开uiautomatorviewer.bat闪退
    常见的python面试题【杭州多测师】【python笔试题】
    支付功能怎么测试?
    自动化运维工具——ansible详解
    服务端编程——异常+校验器+环境变量
    服务端编程——初始koa
    用postman发送请求,在koa中获取的请求body为undefined
    jQuery入口函数测试
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6779950.html
Copyright © 2020-2023  润新知