在npm上发布一个包

在学习ArcGIS JS API的过程中,我尝试自己来实现一个esri/core/Accessor类。所以我这么做了,并且把它做成了一个基类发布到了npm上。

这里主要记录如何发布一个包到npm上。

创建项目

首先创建一个项目,正常的npm init -y初始化就好。然后传到仓库。比如我传到了GitHub。

准备工作

先安装你的依赖(如果你有依赖的话),和填写README.mdLICENSE等。

然后我需要配置package.jsontsconfig.json

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"name": "@geodaoyu/accessor", // 包名,比如要安装当前包,则 npm i @geodaoyu/accessor
"version": "1.0.1", // 版本号,a b c三位
"description": "Implement esri/core/Accessor by myself.", // 描述
"main": "index.js", // 主文件地址
"module": "dist/index.js", // 导出的模块地址
"scripts": { // 脚本命令,我这里只写了两个,通过npm run来执行
"build": "npx tsc", // 打包编译,因为我是ts库
"test": "mocha" // mocha做测试
},
"keywords": [ // 关键字,方便别人检索
"Accessor",
"Esri",
"ArcGIS"
],
"author": "GeoDaoyu", // 作者
"license": "MIT", // 许可
"type": "module", // 类型
"repository": { // git仓库地址
"type": "git",
"url": "https://github.com/GeoDaoyu/Accessor.git"
},
"devDependencies": { // 开发依赖
"mocha": "^8.3.2",
"typescript": "^4.2.4"
}
}

tsconfig.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"compilerOptions": {
"experimentalDecorators": true,
"module": "ESNext",
"target": "ESNext",
"sourceMap": false,
"rootDir": "./src", // 根地址
"outDir": "./dist", // 输出地址
"esModuleInterop": true,
"declaration": true,
"skipLibCheck": true,
"moduleResolution": "node",
},
"include": [
"src/**/*.ts",
],
"exclude": []
}

编写代码

然后就是正常的编写你的代码。调试通过node来调试。

测试代码

编写xxx.test.js等测试代码,然后npm run test来测试。

发布

需要一个npm的账号,然后npm publish即可发布。