美文网首页
发布模块到 npm 仓库

发布模块到 npm 仓库

作者: ihuangyg | 来源:发表于2017-03-21 17:48 被阅读0次

编写模块目录

  • 编写自己模块的 package.json

    npm init
    
  • package.json 内容如下,关于 package.json 的内容编写可以参考该 文章

    {
      "name": "sqlhandler",
      "version": "0.0.1",
      "description": "connect to the sqlite3 and simple curd opertaion ",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "git+ssh://git@github.com/hygfaker/CURDDemo.git"
      },
      "dependencies": {
        "sqlite3": "^3.1.8"
      },
      "keywords": [
        "node.js",
        "sqlite3",
        "javascript"
      ],
      "author": "yans67",
      "license": "MIT",
      "bugs": {
        "url": "https://github.com/hygfaker/CURDDemo/issues"
      },
      "homepage": "https://github.com/hygfaker/CURDDemo#readme"
    }
    
    
  • 在目录中添加 index.js 文件,用于将引用我们写的模块。内容如下:

    module.exports = require('./lib/sqlHandler');
    
  • 创建 lib 目录,用于存放我们的模块。

    npm.png

上传到 npm 仓库

整个目录就这样简单地完成了,接下来就是要将我们的模块推到 npm 上,供他人使用。

  • 在 demo 的根目录上验证我们的 npm 账户,没有账户的话先到 npm 上 注册

    npm adduser
    
  • 完成验证后,就可以 publish 到 npm 仓库上。

    npm publish
    

关于添加依赖时的版本号

  • 指定版本:比如1.2.2,遵循“大版本.次要版本.小版本”的格式规定,安装时只安装指定版本。

  • 波浪号(tilde)+ 指定版本:比如~1.2.2,表示安装1.2.x的最新版本(不低于1.2.2)。

  • 插入号(caret)+ 指定版本:比如ˆ1.2.2,表示安装1.x.x的最新版本(不低于1.2.2),但是不安装2.x.x,也就是说安装时不改变大版本号。需要注意的是,如果大版本号为0,则插入号的行为与波浪号相同,这是因为此时处于开发阶段,即使是次要版本号变动,也可能带来程序的不兼容。

  • latest:安装最新版本。

相关文章

网友评论

      本文标题:发布模块到 npm 仓库

      本文链接:https://www.haomeiwen.com/subject/mlvgnttx.html