准备:

安装node,查看NPM源,npm 发包必须使用 npm 的源镜像,如果你的 npm 源设置了淘宝镜像则需要切换回来

1、查看 npm 源
npm config get registry
2、切换镜像
淘宝镜像:npm config set registry https://registry.npm.taobao.org
npm镜像:npm config set registry https://registry.npmjs.org

1.注册一个账号https://www.npmjs.com,填写邮箱,每次登录都会发送一个一次性密码到你的邮箱;

2.在本地编写模块, npm init 填写相关信息

3.新建index.js文件,编写模块代码

exports.sayHello = function () {
    return "HELLO WORLD";
}

4.终端中登录npm,命令 npm adduser ,输入用户名,密码,邮箱,一次性密码即可,不登录发布会报如下错误:

5.上传包: npm publish . ,包名不可以和现有的npm仓库重名,否则报如下错误,上传成功后就可以在npm上搜索到自己的包文件

6.安装包:npm install 包名