包管理工具
本文最后更新于 2025-01-09,文章内容可能已经过时。
包管理工具,一个集成了众多第三方库资源的管理仓库!
管理工具主要包含了(npm cnpm yarn pnpm)!
代码共享方案
代码共享,就是将自己开发的开源代码解决方案,让更多开发者去发现和使用!
共享的方案有很多,主要包含,官方、 github、或者是npm镜像仓库等!
npm 包管理工具
npm 包管理工具,是NodeJs安装时默认有的依赖仓库管理工具(node package manager),根据npm指定的命令集,对仓库的依赖包进行下载,安装,删除等使用行为!
安装依赖
安装依赖可以通过npm install [包名]的方式来安装第三方依赖库!
npm install dayjs
安装成功后,会在当前文件夹内生成一个文件夹node_modules,该目录下存放下载的各种依赖包文件! 另外还会生成两个文件,一个是package.json另一个是package-lock.json!
package.json:是包管理配置文件,主要有项目的名称、项目的版本号、项目描述、入口文件配置等!
package-lock.json:主要管理各种依赖版本,避免依赖版本混乱!
项目初始化
通过
npm init方式可以根据命令式交互形式,来创建一个项目,最终会将提示内容作为json格式,存放在package.json文件当中!
npm init➜ npm-init npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npm-init)
version: (1.0.0)
description: npm 学习
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/miaojiangwei/Study/FrontEnd/Node/code/包管理工具/npm-init/package.json:
{
"name": "npm-init",
"version": "1.0.0",
"description": "npm 学习",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes){
"name": "npm-init", // 包的名称
"version": "1.0.0", // 包的版本
"description": "npm 学习", // 项目描述
"main": "index.js", // 入口文件
"scripts": { // 脚本配置
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "", // 作者
"license": "ISC" // ISC开源证书
} 最终会将提示信息,记录在
package.json中!通过
npm init -y的形式可以快速创建一个项目,减少命令交互形式内容的填写!
npm init -ypackage.json
常见的属性配置
属性 |
描述 |
|---|---|
name |
项目的名称 |
version |
项目的版本号 |
description |
项目的描述 |
main |
项目的入口文件,如果没有配置,使用require("module")函数引入模块时则会查找该模块目录下的index.js 或者 index.json文件 |
author |
项目的作者 |
license |
开原证书,跟发布代码相关 |
scripts |
配置快捷脚本命令,可以通过npm run [start / restart / dev / stop] 等快捷命令启动和停止项目操作! |
main属性
main是一个入口文件配置,它的值是一个入口文件路径,当通过require引入模块时,会根据配置属性main找到当前的入口文件,如果没有配置,则是当前模块目录下的index.js或者是index.json!
├── index.js
└── node_modules
└── test-module
├── main.js
└── package.json以上是一个
项目模块,在index.js中引入test-module模块中的main.js文件,并执行导出的函数内容!
index.js
const { test } = require("test-module");
test();test-module/main.js
function test() {
console.log('node_modules/test-module/main.js');
}
module.exports = {
test
}test-module/package.json
{
"name": "test-module",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
scripts脚本配置
scripts脚本配置,可以添加快捷指令,将一些复杂的命令行,通过脚本别名的形式,去简化执行命令的操作行为!
node ./code/包管理工具/npm-main/index.js比如我要
执行以上代码,需要输入很长的路径地址并找到index.js文件后,在去执行,比较麻烦!
{
"name": "test-module",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./code/包管理工具/npm-main/index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
在
"scripts"脚本,以键值对的形式,来进行复杂命令配置!
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./code/包管理工具/npm-main/index.js"
}
}
执行命令,使用时只需要如下操作即可,根据指令,会自动匹配并执行所配置的命令行操作!
npm run start至于
run关键字,部分指令在使用时,是可以省略掉的比如start!
npm start除了
start命令外,还有test stop restart命令都是可以省略关键字run的!