前言

在库的开发过程中,如果每次改动我们都将项目发布到npm或者私库上再下载进行调试未面太过于麻烦。通过npm项目中的package.json进行软链接,可以非常方便的看到我们的开发库在被项目使用后的具体情况。


node版本要求

version:16+


案例

1. 建立目录

+ wrap
	|-lib //库
	|-project //项目

2. 初始化

cd wrap/lib;
> wrap/lib npm run init -y
> wrap/project npm run init -y

3.修改project/package.json

{
  "workspaces": [
    "../lib" //路径需要和主相对应,npm i 之后会在当前项目的node_modules中生成一个lib的软连接,指向../lib
  ],
  "keywords": [],
}

3.修改lib项目

//lib
+ |-index.js
//lib/index.js
module.exports=()=>{
	console.log("test index lib")
}
// 修改lib/package.json
{
  "name": "lib",
  "version": "1.0.0",
  "main": "index.js", // 设置lib库的主文件
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {},
  "description": ""
}

4.使用

// project/index.js
const test =require('lib');// 在node_modules中生成的文件名是lib所以直接引入
test();// 使用

思考

1.建立软连接后,使用时引入的包名由谁决定?

在使用时引入的包名由被引用的文件的package.json的name决定

// lib/package.json
{
  "name": "lib", // 由它决定
}
2.更改包名后是否需要刷新包名?

在更改包名之后要执行npm i刷新包名

// lib/package.json
{
  "name": "lib" => "lib-name2"
}
> project 
> npm i || npm i lib-name2 || npm i 其他依赖包名
// project/index.js
const test =require('lib-name2');
test();// 使用
3.更改内容后是否需要手动刷新

只更改内容无需手动刷新,workspaces只是做了一个目录软连接,将文件夹lib软连接至project/node_modules内。而非拷贝了一个副本。

GitHub 加速计划 / js / json
19
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6 binary -> binary_t Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 15 天前
f3dc4684 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 22 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐