How to fix npm ERR! code ENOENT in Docker
This error occurs when npm cannot find a required file, typically package.json, during Docker builds or container execution. The most common cause is volume mounts overwriting files copied during the build process or incorrect WORKDIR/COPY path configurations.
Quick fix
Fixes ENOENT
services:
app:
volumes:
# This mount overwrites /app with your host directory
- ./:/app
# This tries to preserve node_modules but can cause issues
- /app/node_modules- Check if volume mounts are overwriting your files
- Verify Dockerfile COPY paths and WORKDIR
- Check your .dockerignore file
- Verify build context includes your files
services:app:volumes:# This mount overwrites /app with your host directory- ./:/app# This tries to preserve node_modules but can cause issues- /app/node_modulesnpm ERR! code ENOENT
What this error means
The "npm ERR! code ENOENT" error indicates that npm tried to access a file or directory that does not exist. ENOENT stands for "Error NO ENTry" - a standard POSIX error code meaning the specified path could not be found in the filesystem. In Docker contexts, this typically appears as "ENOENT: no such file or directory, open '/app/package.json'" or similar. This error is particularly common in Docker environments because of how container filesystems work. Files copied during image build can be hidden by volume mounts at runtime, or the COPY command in your Dockerfile might not be placing files where you expect. The mismatch between build-time and runtime filesystem states is the root cause in most cases. Unlike on a traditional development machine where paths are straightforward, Docker's layered filesystem and volume mounting system can obscure files that were correctly installed during the build process.
Typical symptoms
- npm install or npm start fails with 'ENOENT: no such file or directory'
- Error message references package.json not found at expected path
- Build succeeds but container fails to start with npm errors
- Same Dockerfile works on some machines but fails on others
- Error appears after adding volume mounts to docker-compose.yml
- Commands work locally but fail in Docker container
Common causes
- Volume mount in docker-compose.yml overwrites the container's working directory
- COPY command in Dockerfile uses incorrect source or destination path
- WORKDIR not set before running npm commands
- package.json excluded by .dockerignore file
- Build context doesn't include the package.json location
- Windows line endings (CRLF) causing path parsing issues
- Docker for Windows folder sharing permissions need to be reset
How to fix "npm ERR! code ENOENT"
The most common cause is volume mounts hiding files that were copied during build. When you mount a host directory to your container's working directory, it replaces everything that was there.
Examine your docker-compose.yml:
services:
app:
volumes:
# This mount overwrites /app with your host directory
- ./:/app
# This tries to preserve node_modules but can cause issues
- /app/node_modulesSolutions:
1. Ensure package.json exists on host: If you're mounting the current directory, make sure package.json is actually there:
ls -la ./package.json2. Use a subdirectory mount instead of mounting at WORKDIR:
volumes:
- ./src:/app/src3. Remove the volume mount for production builds and only use it during development.
Ensure your Dockerfile correctly sets up the working directory and copies files to the right location:
# Set working directory FIRST
WORKDIR /app
# Copy package files relative to WORKDIR
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy rest of application
COPY . .Common mistakes:
# WRONG - COPY before WORKDIR
COPY package.json ./
WORKDIR /app
RUN npm install # npm can't find package.json!
# WRONG - Absolute path without leading slash
COPY package.json app/ # Creates /app/app/package.json
# CORRECT - Use ./ for current WORKDIR
WORKDIR /app
COPY package.json ./
RUN npm installDebug by listing files:
RUN ls -la /appThe .dockerignore file can accidentally exclude package.json from the build context:
cat .dockerignoreMake sure these patterns don't exclude package.json:
# .dockerignore
node_modules
npm-debug.log
.git
.gitignore
# DON'T add these:
# *.json <- This would exclude package.json!
# package*.json <- This would also exclude it!If you're using a wildcard pattern that might match, add an exception:
*.json
!package.json
!package-lock.jsonThe build context is the set of files Docker can access during build. If package.json is outside the context, COPY will fail.
Check your build command:
# Build context is current directory (.)
docker build -t myapp .
# Build context is ./app directory
docker build -t myapp ./appIf your Dockerfile is in a subdirectory but package.json is in the parent:
# WRONG - package.json not in context
cd docker && docker build -t myapp .
# CORRECT - specify context separately from Dockerfile location
docker build -t myapp -f docker/Dockerfile .In docker-compose.yml:
services:
app:
build:
context: . # Build context (where package.json is)
dockerfile: docker/Dockerfile # Dockerfile locationIf you're unsure what files exist in the container, inspect directly:
During build:
WORKDIR /app
COPY package*.json ./
RUN ls -la && cat package.json
RUN npm installIn a running container:
# Start a shell in the container
docker run -it myapp sh
# Or exec into running container
docker exec -it container_name sh
# Then check files
ls -la /app
cat /app/package.jsonCheck if volume is hiding files:
# Run WITHOUT volumes to see if build worked
docker run -it myapp ls -la /app
# Then run WITH volumes to compare
docker-compose run app ls -la /appOn Windows, this error can occur due to Docker Desktop folder sharing or WSL2 issues:
Reset folder sharing:
1. Open Docker Desktop Settings
2. Go to Resources > File Sharing
3. Click "Reset credentials"
4. Re-add your project drive/folder
5. Restart Docker Desktop
For WSL2 users:
# Ensure files are in WSL filesystem, not Windows
# SLOW: /mnt/c/Users/... (Windows filesystem)
# FAST: ~/projects/... (WSL filesystem)
# Move project to WSL filesystem
cp -r /mnt/c/Users/me/project ~/projects/
cd ~/projects/project
docker-compose upFix line endings:
# Convert Windows line endings to Unix
git config --global core.autocrlf input
# Or use dos2unix
dos2unix package.jsonAdvanced notes
Anonymous volumes for node_modules: A common pattern to prevent host node_modules from overwriting container modules can itself cause ENOENT errors:
volumes:
- ./:/app
- /app/node_modules # Anonymous volumeThe anonymous volume /app/node_modules persists between builds. If you delete node_modules locally and rebuild, the old anonymous volume might still be mounted. Fix with:
docker-compose down -v # Remove anonymous volumes
docker-compose up --buildMulti-stage builds: In multi-stage Dockerfiles, ensure you're copying from the correct stage:
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:18-alpine
WORKDIR /app
# Must copy package.json again for production dependencies
COPY package*.json ./
RUN npm install --production
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]npm ci vs npm install: In CI/CD and Docker, prefer npm ci which requires package-lock.json and is faster:
COPY package*.json ./
RUN npm ci --only=productionDebugging build context: To see exactly what files Docker receives:
# Create a Dockerfile that just lists files
echo "FROM busybox" > Dockerfile.debug
echo "COPY . /files" >> Dockerfile.debug
echo "RUN find /files -type f" >> Dockerfile.debug
docker build -f Dockerfile.debug -t debug .Frequently asked questions
- What does "ENOENT" mean?
- The "npm ERR! code ENOENT" error indicates that npm tried to access a file or directory that does not exist. ENOENT stands for "Error NO ENTry" - a standard POSIX error code meaning the specified path could not be found in the filesystem.
- How do I fix "ENOENT"?
- This error occurs when npm cannot find a required file, typically package.json, during Docker builds or container execution. The most common cause is volume mounts overwriting files copied during the build process or incorrect WORKDIR/COPY path configurations.
- What causes "ENOENT"?
- Volume mount in docker-compose.yml overwrites the container's working directory COPY command in Dockerfile uses incorrect source or destination path WORKDIR not set before running npm commands
- Is the "ENOENT" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Docker Official Documentation
- ๐ฌdocker : npm ERR! enoent ENOENT(Stack Overflow)
- ๐ฌ
- ๐ฌdocker-compose fails to start with npm ERR! enoent ENOENT(Stack Overflow)
- ๐ฌ
- ๐npm Common Errors Documentation(Official Docs)
- ๐