当前位置:网站首页>npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
2022-08-01 22:18:00 【朝阳39】
npm 包
npm 包即符合 npm官网 规范的 js 模块,通常会发布到 npm 官网中,供大家下载使用。
npm 包的详细信息
npm 包的详细信息储存在 npm 包的 package.json 文件中,通常包含以下信息:
- name - 包名。
- version - 包的版本号。
- description - 包的描述。
- homepage - 包的官网 url 。
- author - 包的作者姓名。
- contributors - 包的其他贡献者姓名。
- dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
- repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
- main - main 字段指定了程序的主入口文件,require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。
- keywords - 关键字
npm 包的版本号
npm 包的版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。
当代码变更时,版本号按以下原则更新:
- 只是修复bug,需要更新Z位。
- 新增了功能,但是向下兼容,需要更新Y位。
- 有大变动,向下不兼容,需要更新X位。
查看指定包的版本号
grunt 为包名
npm list grunt
安装包 npm install
express 为依赖包的名称,npm install 可简写为 npm i
本地安装
将安装包下载到 ./node_modules 中(运行 npm 命令时所在的目录),若没有 node_modules 目录则会创建该目录
npm install express
使用时,无需指定第三方包路径。
var express = require('express');
全局安装
会将包下载到node.js的安装目录中的 node_modules 目录中,所有项目都可以使用全局安装的包
npm install express -g
更新包 npm update
express 为依赖包的名称
npm update express
搜索包 npm search
express 为依赖包的名称
npm search express
卸载包 npm uninstall
express 为依赖包的名称
npm uninstall express
查看当前目录下的所有包
npm ls
查看全局安装的所有包
npm list -g
开发 npm 包
- 新建文件夹 mypack-test
- 用vscode 打开文件夹 mypack-test
- 终端执行 npm init 生成 package.json 文件,一路回车,最后输入 y 回车即可。
蓝框内的部分为包的信息,可根据需要自行输入(或等生成package.json后再修改)
- package name 包名,默认为项目名称
- version 包的版本号,默认为 1.0.0
- description 包的描述信息
- entry point 包的入口文件(包内的所有内容,要在此文件对外导出),默认为 index.js
- test command 测试命令,默认为 “echo “Error: no test specified” && exit 1”
- git repository 包的远程 git 仓库
- keywords 关键字
- author 包的作者
- license 开源文件协议,默认为 ISC
- 新建文件index.js,内容为
要点:定义的函数/变量一定要对外导出!
/* 函数功能——求和 参数——两个数字 */
function sum(a, b) {
return a + b;
}
module.exports = {
sum: sum,
};
至此,已完成 npm 包的开发,若想对外开放下载,请继续发布 npm 包
发布 npm 包
- 注册 npm 账号
npm adduser
或者直接去官网注册 https://www.npmjs.com/
- 登录 npm 账号
npm login
按提示依次输入npm 的账号、密码、邮箱、邮件中收到的一次性校验码
3. 发布
npm publish
若遇到以下报错
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/mypack-test - You do not have permission to publish "mypack-test". Are you logged in as the correct user?
则说明 npm 官网已存在当前包。
解决方案
将 package.json 中的包名 name 修改为一个 npm 官网不存在的包名,再发布即可。
如最终我的包名为 mypack-test-999
发布成功后,在官网即可查到
https://www.npmjs.com/package/mypack-test-999
试用原创的 npm 包
- 新建文件夹 test_mypack
- 用 vscode 打开文件夹 test_mypack
- 安装包
npm i mypack-test-999
4. 新建文件 index.js,内容为
import {
sum } from "mypack-test-999";
let result = sum(6, 2);
console.log(result);
- 执行 index.js
出现图中的报错,则需要在 package.json 中添加
"type": "module",
再次执行 index.js,得到预期结果,验证成功!
边栏推荐
- Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
- 工程建筑行业数据中台指标分析
- LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- Postman batch test interface detailed tutorial
- 13、学习MySQL 分组
- Use Jenkins for continuous integration, this knowledge point must be mastered
- 毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
- 求解多元多次方程解的个数
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
猜你喜欢
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
Kubernetes第零篇:认识kubernetes
Postman batch test interface detailed tutorial
Still struggling with reporting tool selection?To take a look at this
SOM Network 2: Implementation of the Code
ImportError: `save_weights` requires h5py. Problem solved
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
2022 版 MySQL 巅峰教程,收藏好,慢慢看
APP专项测试:流量测试
(Translation) How the contrasting color of the button guides the user's actions
随机推荐
APP专项测试:流量测试
Kubernetes Scheduler全解析
编曲软件FL studio20.8中文版功能和作用
SAP Spartacus NgExpressEngineDecorator 的工作原理
SOM网络2: 代码的实现
恒星的正方形问题
number of solutions to solve a multivariate multi-degree equation
联邦学习入门
(翻译)按钮的对比色引导用户操作的方式
Recycling rental system 100% open source without encryption Mall + recycling + rental
SOM网络1:原理讲解
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
String - Trie
第一讲 测试知多少
小程序中的多表联合查询
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
字符串——Trie
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
2022-08-01 第八组 曹雨 泛型 枚举