在日常的工作中,我们会接触很多 npm 包。
例如:
npm install jquerynpm install @vue/clinpm install axios# ... 等等
有时候会想到,构建一个属于自己的 npm 包,应该超级酷吧?

[
console.log('hello-tomato7')function sayHello() {console.log('hello-tomato7')
}exports.sayHello = sayHello
// 使用的是 commonjs 规范。

访问npm的官网: 访问npm官网点击这里。注册一个 npm 账号,并登录。


如果英文比较好,npm的官网建议多逛逛,可以发现很多优质的npm依赖包。
!! 需要注意一下,切换我们的下载源为npm官方下载源。
# 1. 下载依赖
npm i -g nrm# 2. 查看下载源列表
nrm ls# 3. 使用npm官方的下载源。
nrm use npm # 4. 验证是否切换成功,查看输出的网址是否为我们设置的网址。
npm get registry

# 1. 使用npm官方的下载源。
npm set registry https://registry.npmjs.org/ # 2. 验证是否切换成功,查看输出的网址是否为我们设置的网址。
npm get registry

npm login

npm publish# publish 出版



可以看到上方的截图,本地初始化一个项目,下载属于我自己的 npm 包
tomato7,在代码中引用并执行。正确输出
hello-tomato7
可以看到例如: vue 项目,主要暴露了 dist 文件,是怎么做到的?

可以借助package.json中的 files 字段
"files": ["src","dist"],
可以借助package.json中的 repository 字段
"repository": {"type": "git","url": "https://github.com/lazy-tomato/tomato7"},
上一篇:描写香菇的句子有哪些?
下一篇:第一个Shader程序