yarn 版本
version:1.22.4
可使用npm i yarn -g 升级或安装
Upgrading Yarn itself:yarn set version latest
创建一个项目作为根项目
#zsh
mkdir yarn-workspace-test
cd yarn-workspace-test
yarn init -y
创建两个package
mkdir packages && cd packages
mkdir common
mkdir server
cd common
yarn init -y
cd ../server
yarn init -y
修改根项目的package.json
{
....
"private": true,
"workspaces": [
"packages/*"
],
...
}
实现server包中引用common中的模块
cd packages/common
touch index.js
cd ../server
touch index.js
common/index.js
module.exports = ( ) => console.log('This is a function in common');
server/index.js
const func = require('common');
func()
安装依赖并运行
yarn install
node packages/server/index.js
看到输出:This is a function in common
在server或common下安装依赖
common和server目录下,没有node_modules目录,依赖安装到了跟项目的node_modules下
cd packages/server
yarn add jest
ls
未发现node_modules