当前位置:网站首页>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,得到预期结果,验证成功!
边栏推荐
- xctf攻防世界 Web高手进阶区 web2
- leetcode 204. Count Primes 计数质数 (Easy)
- Analysis of the development trend of game metaverse
- Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
- Safe fifth after-school exercise
- NgRx Store createSelector 的单步调试和源代码分析
- The must-have "fishing artifact" for programmers is here!
- KMP 字符串匹配问题
- 递归(各经典例题分析)
- SOM网络1:原理讲解
猜你喜欢
随机推荐
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
Use Jenkins for continuous integration, this knowledge point must be mastered
字符串——Trie
Safe fifth after-school exercise
越长大越孤单
第一讲 测试知多少
使用Jenkins做持续集成,这个知识点必须要掌握
联邦学习在金融领域的发展和应用
Today's sleep quality record 74 points
Kubernetes Scheduler全解析
1. @Component注解的原理剖析
blender3.2.1 unit setting
User Experience | How to Measure User Experience?
SAP Spartacus Accessibility E2E 端到端测试
华为无线设备配置全局双链路冷备份(AC全局配置方式)
将vim与系统剪贴板的交互使用
NgRx Store createSelector 的单步调试和源代码分析
46.全排列
递归(各经典例题分析)
【开源】Sentinel高性能高可用集群限流解决方案