npm发布包
准备工作
发布包
- npm官方建议规范的包至少包含:
- package.json(包的基本信息)
- README.md(文档)
- index.js (入口文件)
第一步:创建项目
(1)创建工程文件夹
mkdir test-package && cd test-package
(2)创建package.json
npm init
注意:本次演示的包的入口文件是index.js,请务必确保package.json中字段main对应的值是“index.js”。
最终结果:
{
"name": "test-package",
"version": "1.0.0",
"description": "test npm package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"npm",
"packge"
],
"author": "yuyy",
"license": "ISC"
}
(3)创建README.md
内容:
### test-package
This is test-package npm package!
It is just for learning.
(4)创建index.js
内容:
module.exports = {
printMsg: function () {
console.log('this message is from test-package!');
}
}
最终的目录结构:
└── test-package
├── README.md
├── index.js
└── package.json
第二步:发布
npm publish