在npm上发布模块包

140 阅读1分钟

1.注册自己的npm账号

npm官网:www.npmjs.com/

2.登录npm账户

image.png

3.开发一个模块包

  • 新建一个文件夹
  • 初始化npm工程
npm init -yes
  • 安装依赖包:
npm i
  • 配置文件:package.json
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.21"
  }
}
  • 入口文件添加要暴露的api
// index.js
module.exports = {
  方法名,
};

4.注册npm账户(要发布的包下面的命令行中)

注意: 查看当前npm镜像源:

npm config get registry

npm淘宝镜像源是:registry.npm.taobao.org/

这一步需要npm的官方源:

  • 配置npm官方源:
npm config set registry https://registry.npmjs.org/
  • 查看是否配置成功:
npm config get registry
  • 添加要发布包到的npm账户
npm adduser
  • 输入个人npm账户信息
// 用户名
Username: 
// 密码
Password:
// 邮箱
Email: (this IS public) ldf6780@163.com
// 到这一步就添加账户成功了:
Logged in as 

5.上传包到npm

执行:

$ npm publish

npm notice 
npm notice 📦  lmod@1.0.0
npm notice === Tarball Contents ===
npm notice 140B index.js
npm notice 268B package.json
npm notice === Tarball Details ===
npm notice name:          lmod
npm notice version:       1.0.0
npm notice filename:      lmod-1.0.0.tgz
npm notice package size:  377 B
npm notice unpacked size: 408 B
npm notice shasum:        5e98b9f7b38278a25ac7f6ca4751c2f2368a484b
npm notice integrity:     sha512-fHiPg+4yqBotN[...]bqOzdtMCVqSKA==
npm notice total files:   2
npm notice
+ lmod@1.0.0

到如上一步就发布npm包成功了,可以在npm网站上搜索发布的包。

image.png

使用发布成功npm包

  • npm包发布成功变了已向其他包一样安装引入使用了。
npm install lmod --save