当前位置:网站首页>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,得到预期结果,验证成功!
边栏推荐
- Kotlin interface generic covariant inversion
- Integrated API interface code of domestic express companies for intra city distribution and ordering - Express 100
- 美国会参议院推进两党枪支安全法案
- What is the difference between sap QM and UD for inspection lots with hum?
- 群晖向阿里云OSS同步
- The hidden corner of codefarming: five things that developers hate most
- Preparation and operation & Maintenance Guide for 'high concurrency & high performance & high availability service program'
- 2022年施工升降机司机(建筑特殊工种)考试试题及在线模拟考试
- How to create a new empty branch in the web development process of easyrtc?
- Android kotlin 大全
猜你喜欢

Developer survey: rust/postgresql is the most popular, and PHP salary is low

One article explains R & D efficiency! Your concerns are

**Unity中莫名其妙得小问题-灯光和天空盒

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

吉时利静电计宽测量范围

The research on the report "market insight into China's database security capabilities, 2022" was officially launched

黄金年代入场券之《Web3.0安全手册》

Beauty of script │ VBS introduction interactive practice

Golden age ticket: Web3.0 Security Manual

Seven challenges faced by data scientists and Solutions
随机推荐
Gateway processing flow of zuul source code analysis
吉时利静电计宽测量范围
Docker安装redis
金鱼哥RHCA回忆录:DO447管理项目和开展作业--为ansible剧本创建一个项目
Party, Google's autoregressive Wensheng graph model
这几个默认路由、静态路由的配置部署都不会,还算什么网工!
Why did the audio and video based cloud conference usher in a big explosion of development?
TCP triple handshake
Activity生命周期
群晖向阿里云OSS同步
[one picture series] one picture to understand Tencent Qianfan apaas
Comparator sort functional interface
CPU process priority
kotlin 协程 lanch 详解
Can inspection results be entered after the completion of inspection lot UD with long-term inspection characteristics in SAP QM?
Troubleshooting the kubernetes problem: deleting the rancher's namespace by mistake causes the node to be emptied
不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
CVPR 2022 | 美团技术团队精选论文解读
kotlin 协程上下文和调度器
On the difference between process and thread