创建npm账号
1.登录npm官网
如果之前没有npm账号,点击
2.根据官方文档来设置2FA,第六步这里要求我们 在手机上打开验证器应用程序,然后在两步验证页面上,用手机扫描二维码。
我们需要在手机上下载一个,然后扫码获得六位的验证码(这个工具在发布npm包的时候也会用到)
创建npm包
1.新建一个mul-speech文件夹
2.cd mul-speech
3.npm init
{
"name": "mul-speech", // 包名,不可重复
"version": "1.0.0", // 版本号
"author": "xxx", // 作者
"description": "common toolkit", // 描述信息
"keywords": ["utils", "format", "money", "phone"], // 关键词,提升SEO
"entry point": (index.js) //入口文件
"license": "ISC", // 许可证
"scripts": {
// 存放可执行脚本
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
// 运行依赖
},
"devDependencies": {
// 开发依赖
}
}
4.在index.js文件中暴漏出你的方法或者类。
上传github
1.新建一个名为mul-speech的仓库,然后根据文档上传你的文件。
发布npm包
1.检查 npm 源,如果是淘宝源,则需要改回 npm 源
// 查看npm镜像源地址
npm config get registry
// 切换npm镜像源
// 设置npm默认源
npm config set registry https://registry.npmjs.org/
// 设置npm镜像源为淘宝镜像
npm config set registry https://registry.npm.taobao.org/
2.切换到项目下,输入账号密码邮箱等,密码输入也不会显示的 在Enter one-time password这一步,打开手机上我们下载的软件,再次输入6位的密钥。
// 登陆
npm login
// 控制台会提示输入相关信息
Log in on https://registry.npmjs.org/
Username: // 用户名
Password: // 密码
Email: (this IS public) // 邮箱
Enter one-time password: // 如果之前做过 双因素身份验证 (2FA),需要生成一次性密钥
Logged in as xxx on https://registry.npmjs.org/.
3.npm publish
自己在项目中用到了,就在掘金上搜了下,参照原文juejin.cn/post/703914…,此文添加了我在发布时候遇到的一些问题。