一,创建npm库

1,代码

a, toolkit.js

export class Toolkit{
    public printSomething(): void{
        console.log(`this is ts npm lib`);
    }
}

b, index.js

import { Toolkit } from "./toolkit";
export {Toolkit};

2, 编辑tsconfig.jsom

{
  "compilerOptions": {
			 "target": "es5",                          
       "module": "commonjs",                    
       "declaration": true,                     
       "outDir": "./dist",                        
       "strict": true,                           
       "esModuleInterop": true,
       "forceConsistentCasingInFileNames": true  
  },
  "include": [
       "src/**/*.ts"
     ],
  "exclude": [
       "node_modules",
       "**/*.spec.ts"
     ]
}

3, 命令:tsc

Cocos Creator 3.x之自定义npm库(ts版)_npm

4, 命令:npm publish, 提交库

Cocos Creator 3.x之自定义npm库(ts版)_ts_02

二,nodejs平台测试

1, 命令:npm i npmts-demo-alex

2, 测试代码index.js

// "use strict"
// exports.__esModule = true;

let npm_1 = require("npmts-demo-alex");
let alex = new npm_1.Toolkit();
alex.printSomething();

3, 结果

Cocos Creator 3.x之自定义npm库(ts版)_库_03

三,cocos creator 3.x平台测试

   ```json
   {
     "compilerOptions": {
       "moduleResolution": "node",
       "esModuleInterop": true,
       ...
     },
     ...
   }
   ```

1,命令:npm i npmts-demo-alex

2,代码

Cocos Creator 3.x之自定义npm库(ts版)_ts_04

3, 结果

Cocos Creator 3.x之自定义npm库(ts版)_库_05