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

➜ mkdir yarn-classic-workspaces-example

➜ cd yarn-classic-workspaces-example

➜ yarn init -y -p

➜ cat package.json
{
  "name": "yarn-classic-workspaces-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true
}

Create Workspace

➜ mkdir packages packages/a packages/b

➜ cd package/a

➜ yarn init -y

➜ cd ../b

➜ yarn init -y

➜ cd ...

➜ code .
{
  "name": "yarn-classic-workspaces-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}
➜ yarn

특정 workspace 에 외부 의존성 추가

➜ yarn workspace a add axios
// yarn-classic-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-classic-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(", "));
})();

workspace 스크립트 실행하기

{
  "name": "a",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "axios": "^1.5.1"
  },
  "scripts": {
    "start": "node index.js"
  }
}