Sub topic 15
Troubleshooting checks that fail only in CI
This chapter assumes you can read a basic GitHub Actions workflow, run ESLint and Prettier (or Ruff) locally, and use Git. When a check passes on your machine but fails in CI, the cause is almost always a difference between the two environments. Find that difference and the fix is usually short.
The working model
CI starts from a clean checkout on a fresh runner. It installs dependencies from the lockfile, runs the same scripts you defined, and fails the job on any non-zero exit code. So a CI-only failure means one of four inputs differs: the files, the dependencies, the tool versions, or the configuration.
Common causes
| Symptom in CI log | Likely cause | Fix |
|---|---|---|
| `Cannot find module` or `command not found` | Package is installed globally on your machine but missing from `package.json` | Add it to `devDependencies` and commit the lockfile |
| Different rule results | Local and CI tool versions differ, or `npm install` changed versions | Pin versions and use `npm ci` |
| Every file fails the format check | Line endings differ (CRLF on Windows, LF on the runner) | Set `* text=auto eol=lf` in `.gitattributes` and configure `endOfLine` |
| Files you never touched are flagged | CI checks the whole repository, while you only checked changed files | Run the same `npm run lint` locally |
| Config not found | Config file is untracked, ignored, or in a different directory | Commit it, or set `working-directory` |
| Passes after a re-run, fails again later | Stale cache | Change the cache key or clear the cache |
Reproduce CI locally
Do not guess. Recreate the CI steps in a clean state:
rm -rf node_modules
npm ci
npm run lint
npm run format:checknpm ci installs exactly what the lockfile lists and fails if package.json and the lockfile disagree. That mismatch is a frequent CI-only error, so seeing it locally confirms the cause.
Also compare versions. Print them in the workflow so the log shows what CI actually ran:
- run: node --version
- run: npx eslint --version
- run: npx prettier --versionIf your local Node differs from node-version: 20, align them. You can add an .nvmrc file and use node-version-file: .nvmrc in actions/setup-node so both environments read one source.
Line endings in detail
Prettier's endOfLine option defaults to lf. A Windows checkout with core.autocrlf=true converts files to CRLF, so Prettier flags them locally while CI, running on Linux, sees LF. A .gitattributes file removes the ambiguity:
* text=auto eol=lfWorking directory and case sensitivity
Linux runners have case-sensitive file systems. An import of ./Button that resolves to button.js works on macOS and Windows but fails on Linux. Rename the file with git mv so Git records the case change.
In a monorepo, the config may live in a subfolder. Set the directory for a step like this:
- run: npm run lint
working-directory: webStale caches
A cache keyed only on the OS can restore old packages. actions/setup-node with cache: npm keys on the lockfile hash, so it normally refreshes when the lockfile changes. If you suspect a bad cache, delete it from the repository's Actions caches page and re-run the job.
Exercise
Your format check passes locally on Windows, but CI reports that nearly every file is not formatted. Name the most likely cause and two changes that fix it.
Show answer
Line endings differ: local files use CRLF and the Linux runner expects LF, or the reverse. Add * text=auto eol=lf to .gitattributes and make sure Prettier's endOfLine is lf. Then renormalize the files and commit.
Exercise
ESLint reports no errors locally but CI fails with a rule error. Which commands help you find out whether versions or dependencies differ?
Show answer
Run rm -rf node_modules && npm ci locally to match the lockfile, then compare npx eslint --version and node --version with the values printed in the CI log.