Workspaces | Yarn

Node.js

# Node.js 버전 확인
➜ node -v
v20.9.0

# v20.9.0 가 설치되어 있지 않다면
➜ nvm install 20.9.0

# v20.9.0 를 사용하고 있지 않다면
➜ nvm use 20.9.0

# Node.js 버전 다시 확인
➜ node -v
v20.9.0

npm

# npm 버전 확인
➜ npm -v
10.2.1

# 최신 버전 npm 으로 업그레이드 
➜ npm i [email protected] -g

# npm 버전 다시 확인
➜ npm -v
10.2.1

yarn

# yarn 설치 확인 및 버전 확인
➜ yarn -v
1.22.19

# 최신 버전 npm 으로 업그레이드 
➜ npm i [email protected] -g

# yarn 설치 다시 확인 및 버전 다시 확인
➜ yarn -v
1.22.19

Create Workspaces Root with Yarn berry

➜ mkdir yarn-berry-workspaces-example

➜ cd yarn-berry-workspaces-example

# Added in: v16.9.0, v14.19.0
➜ corepack enable

➜ yarn init -2 -w

➜ cat package.json
{
  "name": "yarn-berry-workspaces-example",
  "packageManager": "[email protected]",
  "workspaces": [
    "packages/*"
  ]
}

Create Workspace

➜ mkdir packages/a packages/b

➜ cd package/a

➜ yarn init

➜ cd ../b

➜ yarn init

➜ cd ...

➜ code .
{
  "name": "b",
  "packageManager": "[email protected]",
  "main": "index.js"
}

특정 workspace 에 외부 의존성 추가

➜ yarn workspace a add axios

의존성 파일이 없는 경우

# 
➜ yarn config enableGlobalCache

➜ yarn config set enableGlobalCache false

➜ yarn

➜ cat .yarnrc.yml

workspace 코드 작성

// yarn-berry-workspaces-example/package-b/index.js

const axios = require("axios");

module.exports = async function () {
  const response = await axios.get("<https://api.github.com/users>");

  return response.data;
};
// yarn-berry-workspaces-example/package-a/index.js

const b = require("b");

(async function main() {
  const users = await b();

  console.log(users.map((user) => user.login).join(", "));
})();