一,注册一个npm账号
网址: npmjs.com
二, 开始制作npm库
1,新建一个文件夹,如npmDemo
2,进入npmDemo, 执行命令npm init -y
这样在npmDemo中就会生成一个package.json的配置文件
{
"name": "npmDemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
3, 创建一个入口js文件
如package.json, "main": "index.js"说明index.js是入口文件,当然我们可以更改在个文件的名称,现在就用这个默认的index.js, index.js代码内容如下
exports.testDemo = function() {
console.log("this is test demo!!!!!!");
};
4, 项目结构如下
5, 发布npm包
a, 登录到npm账户, 命令:npm adduser
b, 发布到npm仓库中, 命令:npm publish
⚠️可能的出错:
这表示: 你的库名有重复的,在packege.json中更改你的库名为:alexhu-npm-demo
{
"name": "alexhu-npm-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
6,查看自己是否发布成功
a,登录到npm官网,查看是否有这个库
三, 在Cocos Creator 3.x中使用
1, 配置tsconfig.json
{
/* Base configuration. Do not edit this field. */
"extends": "./temp/tsconfig.cocos.json",
/* Add your custom configuration here. */
"compilerOptions": {
"strict": false,
"target": "es6",
"module": "commonjs",
"experimentalDecorators": true,
"strictNullChecks": false,
"allowSyntheticDefaultImports": true
}
}