On our local machines, we’ve installed the latest version of Yarn for our node version of 16.17
per the instructions on the Yarn website:
corepack enable
corepack prepare yarn@stable --activate
We found this step was necessary to make it work locally as well:
corepack prepare yarn@<version> --activate
We use Github actions to do things like lint and test our code. Here’s the lint.yml file:
name: Lint
on:
# Trigger the workflow on push or pull request,
# but only for the main branch
push:
branches:
- main
pull_request:
branches:
- main
jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "16.17"
- name: Install Node.js dependencies
run: yarn install --immutable --immutable-cache --check-cache
- name: Run eslint
run: yarn eslint src/ --max-warnings=0
- name: Run Prettier
run: yarn prettier src/ --check
According to the Github Actions docs, yarn should be installed in this CI environment:
GitHub-hosted runners have npm and Yarn dependency managers installed. You can use npm and Yarn to install dependencies in your workflow before building and testing your code.
However I believe the Yarn version Github actions uses is 1.something, as it seems to be the one installed when yarn
is installed with npm install -g yarn
, the old method of installing Yarn.
As such, we get the following error when Github actions tries to use yarn
:
output from github CI
node:internal/modules/cjs/loader:959
throw err;
^
Error: Cannot find module '/home/runner/work/ui/ui/.yarn/releases/yarn-3.5.0.cjs'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
at Function.Module._load (node:internal/modules/cjs/loader:804:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
I’ve tried various methods recommended online for telling Github which version of node and yarn to use. For example, I’ve added packageManager
property to our package.json
:
package.json
...
"packageManager": "[email protected]",
...
That didn’t work, so I added engines
property to package.json
:
package.json
...
"engines": {
"yarn": ">=3.5.0"
}
The error remained the same, so I tried directly installing and activating the latest version of yarn in the github actions .yml
file:
lint.yml
name: Lint
on:
# Trigger the workflow on push or pull request,
# but only for the main branch
push:
branches:
- main
pull_request:
branches:
- main
jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "16.17"
- name: Install latest Yarn
run: corepack prepare yarn@stable --activate
- name: Activate latest Yarn
run: yarn set version stable
- name: Install Node.js dependencies
run: yarn install --immutable --immutable-cache --check-cache
- name: Run eslint
run: yarn eslint src/ --max-warnings=0
- name: Run Prettier
run: yarn prettier src/ --check
I can’t be entirely certain this error is because of a failure to use yarn v3.x, but that’s my suspicion as this whole setup worked with our yarn.lock
was in the yarn v1.x format and we all used yarn v1.x locally. It’s only with the upgrade that it fails to work on github actions.
How can I instruct Github Actions’ runner to find / use the latest version of Yarn?
Additional Info:
.yarnrc.yml
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-3.5.0.cjs
2
Answers
this was help me (NOT SURE THAT THIS IS CORRECT WAY)
yarn by default added in gitignore .yarn (full folder),
but i decided to add only .yarn/cache folder in .gitignore, not full .yarn.
now my ci is working.
gitignore in root of monorepo
It looks to me that you’ve gitignored too much. This is the recommended setup from Yarn docs:
https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
I was facing similar problems when I was ignoring
.yarn/
instead of.yarn/*
with the ignores to that rule followed after as recommended in the docs.