如何把 nodejs 模块发布到 npm

node 开发者习惯使用 npm 来管理模块. 当我们把代码发布到 npm 不但方便自己在项目中 require 进来, 还可以把自己的代码分享给别人.
这里略过如何编写一个 node 模块, 因为不是本文的重点. 如果还不清楚的可以参考 npm 的官方文档  https://docs.npmjs.com/getting-started/creating-node-modules
首先需要一个 npm 的账号. 可以在 https://www.npmjs.com/ 网上注册, 或者 npm adduser
然后登陆这个账号 npm login, 按照提示填入用户名, 密码, 邮箱
准备工作已经完成, 现在来做实际的发布工作.
假设现在有一个 node 模块 smmsx, 并且在模块的根目录. 执行 npm init 生成 package.json 模板.
修改 package.json 文件. 这里包含了模块的基本信息, 最主要是
  • name: 模块名称, require 需用到
  • version: 版本号
  • description: 描述
  • main: 主文件
  • scripts: 略过
  • keywords: 略过
  • author: 略过
  • license: 略过
  • bugs: 略过
  • homepage: 略过

例子:
{
    "name": "smmsx",
    "description": "interfaces of sm.ms",
    "license": "Apache-2.0",
    "homepage": " https://gitee.com/csling/smmsx",
    "main": "index.js",
    "repository": {
            "type": "git",
            "url": "git+ https://gitee.com/csling/smmsx.git"
      },
    "maintainers": [
        {
            "name": "linglq",
            "email": " [email protected]"
        }
    ],
    "dependencies": {
        "gm": "^1.23.1",
        "request": "^2.83.0",
        "request-promise": "^4.2.2"
    },
    "version": "0.0.1"
}


一键发布: npm publish. 提示完成后就可以在 npm 上找到了. 例如 https://www.npmjs.com/package/smmsx
当有新版本发布时, 用命令 npm version <type> 更新版本号, type 可以是 major, minor, patch, 分别对应大版本号, 小版本号, 补丁修正版本号(版本号形如 10.1.2)
而后依然用 npm publish 发布新代码到 npm 代管.

猜你喜欢

转载自blog.csdn.net/afeiqiang/article/details/79403646