当前位置:网站首页>npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
2022-06-24 12:58: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,得到预期结果,验证成功!
边栏推荐
- Vulnerability management mistakes that CIOs still make
- Who is the fish and who is the bait? Summary of honeypot recognition methods from the perspective of red team
- 3. caller service call - dapr
- The introduction of MySQL memory parameters is divided into two categories: thread exclusive and global sharing
- kotlin 协程 lanch 详解
- Usage of multimeter
- Coinbase将推出首个针对个人投资者的加密衍生产品
- 如何避免严重网络安全事故的发生?
- Eight major trends in the industrial Internet of things (iiot)
- Geological disaster early warning monitoring RTU
猜你喜欢
Party, Google's autoregressive Wensheng graph model
Hands on data analysis unit 3 model building and evaluation
hands-on-data-analysis 第三单元 模型搭建和评估
How to avoid serious network security accidents?
Opengauss kernel: simple query execution
3. caller service call - dapr
Activity生命周期
Seven challenges faced by data scientists and Solutions
吉时利静电计宽测量范围
《中国数据库安全能力市场洞察,2022》报告研究正式启动
随机推荐
Prometheus PushGateway 碎碎念
**Unity中莫名其妙得小问题-灯光和天空盒
Kotlin initialization block
Eight major trends in the industrial Internet of things (iiot)
kotlin 接口 泛型 协变 逆变
[sdx62] wcn685x IPA failure analysis and solution
8 lines of code to teach you how to build an intelligent robot platform
AGCO AI frontier promotion (6.24)
redis 数据类型详解
Opengauss kernel: simple query execution
SAP QM qac1 transaction code cannot modify the quantity in the inspection lot containing Hu
[AI player cultivation record] use AI to identify what kind of wealth is next door
System status identifier 'hum' for SAP QM inspection lot
Integrated API interface code of domestic express companies for intra city distribution and ordering - Express 100
数据科学家面临的七大挑战及解决方法
Coinbase将推出首个针对个人投资者的加密衍生产品
Google Earth Engine——1999-2019年墨累全球潮汐湿地变化 v1 数据集
Without home assistant, zhiting can also open source access homekit and green rice devices?
One hour is worth seven days! Ingenuity in the work of programmers
Comparator sort functional interface