开发npm包简单流程

开发一个npm包

1. 注册npm账号

2. 配置package.json

$ mkdir modal-helper
$ cd modal-helper
$ npm init --y # 初始化
// package.json
{
  "name": "modal-helper",
  "version": "1.0.0",           // 大迭代.功能叠加.修复bug
  "description": "防止弹框滚动穿透的一种解决方式",     // 描述信息
  "main": "index.js",           // 入口文件名
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"    // 测试命令(可以不写)
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/wywppkd/modal-helper.git"  // github仓库地址
  },
  "keywords": [                // 关键词
    "关键词1",                  
    "关键词2",
    "关键词3"
  ],
  "author": "wywppkd",        // 作者
  "license": "MIT",           // 开源协议
  "bugs": {
    "url": "https://github.com/wywppkd/modal-helper/issues"
  },
  "homepage": "https://github.com/wywppkd/modal-helper#readme"
}

name包命名规范

  1. 不能以点或下划线开头
  2. 不能包含大写字母
  3. 不能包含任何非URL安全字符(因为包名最终会成为URL的一部分)
  4. 小于或等于214个字符
  5. 不可以与现有包名类似:
    • 与react-native包冲突的命名
      • reactnative
      • react_native
      • eact.native
  6. 推荐命名:
    • 普通命名: modal-helper
    • 作用域命名: @wywppkd/modal-helper

4. 开发你的包…

5. 将开发的代码上传到npm官方服务器

注意需要保证npm源为官方地址

$ npm login  # 登录npm账户
    # 输入账户密码邮箱
$ npm publish # 上传代码(普通包名)
$ npm publish --access=public # 上传代码(如果是作用域命名的包@wywppkd/modal-helper)

尝试下载

$ npm install modal-helper --save # 下载
  # 整个目录下文件都会下载至node_modules/modal-helper

迭代npm包

  • 每次迭代后必须修改package.json的version(版本号)属性