Skip to content

npm 发布备忘录

这里记了一些发布 npm 包要注意的事项

包名不要有大写字母

创建

bash
npm init -y
# 如果是ts项目
tsc --init

package.json

json
{
  "name": "opchatjs-weblogger",
  // 版本号每次发布都要改
  "version": "0.0.1",
  "description": "用于浏览器的日志,支持彩色输出",
  "main": "index.js",
  // 如果是ts项目,这里要写编译后的js的d.ts文件
  "typs": "index.d.ts",
  "scripts": {},
  "keywords": [
    // 关键字随意
  ],
  // 作者(必填)
  "author": "Truraly",
  // 开源协议
  "license": "MIT",
  // 如果你发布的是组织的包,要加上这个,不然npm发布会报错
  "publishConfig": {
    "access": "public",
    "registry": "https://registry.npmjs.org/"
  }
}

开源协议使用方法

参考资料:

MIT | 麻省理工学院许可协议

麻省理工学院许可协议为例,将下面的内容复制到项目根目录的LICENSE文件中

如有需要可以再放到NOTICE文件中(可选)

txt
Copyright <这里改成年份> <这里改成你的名字>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

.npmignore

选择性的忽略文件,以下为个人常用的配置

bash
# .npmignore

/node_modules
/package-lock.json
/.gitignore # 注意.gitignore里要把这行删掉
/index.ts
/tsconfig.json
/src
/docs/.vitepress/dist
/docs/.vitepress/cache
*dist/
*log/
.VSCodeCounter

tsconfig.json

如果是 TS 项目,注意打开declaration选项,这样才能生成.d.ts文件

json
{
  "compilerOptions": {
    // ...
    "declaration": true
  }
}

发布

bash
npm login
npm publish

常用插件

bash
npm i -D esbuild-register
npm i -D @types/log4js
npm i log4js

相关资料

Copyright © 2022 田园幻想乡 浙ICP备2021038778号-1