Jenkins dir step "no such file or directory" / wrong CWD in CI
The dir(path) step changes the working directory for the steps it wraps. If that path was never created (no checkout there, no mkdir), shell commands inside the block run against a missing directory and fail.
What this error means
A sh inside dir('subdir') fails with sh: 0: can't open ... or No such file or directory, or files land in an unexpected location because the directory did not exist.
[Pipeline] dir
Running in /home/jenkins/workspace/app/frontend
[Pipeline] sh
+ npm ci
npm error code ENOENT
npm error syscall open
npm error path /home/jenkins/workspace/app/frontend/package.json
npm error errno -2Common causes
The target directory was never created
dir enters the path but does not create the source there; if no checkout or build produced it, the directory is empty or absent.
A monorepo subdir mismatch
The block points at a subproject path that differs from the actual repository layout, so expected files are missing.
How to fix it
Create or check out the directory before entering it
- Confirm the path exists with
fileExistsafter checkout. - Run
checkout/gitso the expected files are present, ormkdir -pthe directory you write to. - Match the
dirpath exactly to the repository layout.
dir('frontend') {
sh 'ls -la'
sh 'npm ci'
}How to prevent it
- Check out source before entering a subdirectory with
dir. - Use
fileExiststo validate a path before running steps in it. - Keep
dirpaths in sync with the actual repo structure.