很多人都还没有自己发布过npm包,今天我就来讲解一下,写一个最简单的npm包,并发布上去。
mkdir my-npm-demo
cd my-npm-demo
npm init
新建入口文件index.js, 里面我们写个小函数,
module.exports = function(req) {
console.log(req);
}
注意: 在package.json里,main必须是入口文件
name:包的名字,确保该名字是独一无二的
version:包的版本,默认是1.0.0
description:包的描述
main:入口文件,默认是index.js
test command:测试命令
repository:git仓库地址,一般为”type”:”git”,”url”:”git的url”
keyword:这个挺重要,尽量用比较贴切的关键字作为这个包的索引,这样才能有更多的人搜索到你的包
author:写你的账号或者你的github账号吧
license:开源协议用了哪个 建议用:MIT
参考链接:https://spdx.org/licenses/MIT... & https://opensource.org/licens...
新建文件名称为:LICENSE
没有后缀
内容输入参考链接里面的text内容即可。
将<year> 和 <copyright holders>修改为 对应的年份 和 版权拥有者名字
例子:
The MIT License (MIT)
Copyright (c) 2021-present xiazhipeng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
https://www.npmjs.com
npm adduser
;然后输入npm whoami
,如果出现注册名字,说明成功npm publish --access=public
,即可发布成功npm install
你的包名,或者去注册账户查看好了,到这里第一个npm包就发布成功了,恭喜