1.初始化package包
npm init -y
{
"name": "common-cjs-tools",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "常用工具方法"
}
2.新建index.js文件
3.写两个工具方法
function addFn(...args) {
return args.reduce((acc, curr) => acc + curr, 0);
}
function multiplyFn(...args) {
return args.reduce((acc, curr) => acc * curr, 1);
}
module.exports = { addFn, multiplyFn };
4. 本地测试下(node环境用cjs)
5.登录npm账号
npm adduser
回车跳转到浏览器登录
6发布到npm官网
npm publish
7.到npm查看官网
8.使用npm
npm i common-cjs-tools
const { addFn, multiplyFn } = require("common-cjs-tools");
console.log(addFn(1, 2, 3, 4, 5));
console.log(multiplyFn(1 * 2 * 3 * 4));