Skip to content
Latchkey

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.

Jenkins console
[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 -2

Common 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

  1. Confirm the path exists with fileExists after checkout.
  2. Run checkout/git so the expected files are present, or mkdir -p the directory you write to.
  3. Match the dir path exactly to the repository layout.
Jenkinsfile
dir('frontend') {
  sh 'ls -la'
  sh 'npm ci'
}

How to prevent it

  • Check out source before entering a subdirectory with dir.
  • Use fileExists to validate a path before running steps in it.
  • Keep dir paths in sync with the actual repo structure.

Frequently asked questions

What causes ""cd: no such file or directory" in dir"?
dir enters the path but does not create the source there; if no checkout or build produced it, the directory is empty or absent.
How do I fix "cd: no such file or directory" in dir?
Create or check out the directory before entering it

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card