How to fix npm ERR! code ENOENT in Docker

DockerINTERMEDIATEMEDIUMUpdated Dec 6, 2025ยท 181 views

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
  1. Check if volume mounts are overwriting your files
  2. Verify Dockerfile COPY paths and WORKDIR
  3. Check your .dockerignore file
  4. Verify build context includes your files
docker โ€” error
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
npm 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

  1. Volume mount in docker-compose.yml overwrites the container's working directory
  2. COPY command in Dockerfile uses incorrect source or destination path
  3. WORKDIR not set before running npm commands
  4. package.json excluded by .dockerignore file
  5. Build context doesn't include the package.json location
  6. Windows line endings (CRLF) causing path parsing issues
  7. Docker for Windows folder sharing permissions need to be reset

How to fix "npm ERR! code ENOENT"

1Check if volume mounts are overwriting your files

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:

yaml
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

Solutions:

1. Ensure package.json exists on host: If you're mounting the current directory, make sure package.json is actually there:

bash
ls -la ./package.json

2. Use a subdirectory mount instead of mounting at WORKDIR:

yaml
volumes:
  - ./src:/app/src

3. Remove the volume mount for production builds and only use it during development.

2Verify Dockerfile COPY paths and WORKDIR

Ensure your Dockerfile correctly sets up the working directory and copies files to the right location:

dockerfile
# 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:

dockerfile
# 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 install

Debug by listing files:

dockerfile
RUN ls -la /app
3Check your .dockerignore file

The .dockerignore file can accidentally exclude package.json from the build context:

bash
cat .dockerignore

Make sure these patterns don't exclude package.json:

bash
# .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:

bash
*.json
!package.json
!package-lock.json
4Verify build context includes your files

The 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:

bash
# Build context is current directory (.)
docker build -t myapp .

# Build context is ./app directory
docker build -t myapp ./app

If your Dockerfile is in a subdirectory but package.json is in the parent:

bash
# 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:

yaml
services:
  app:
    build:
      context: .           # Build context (where package.json is)
      dockerfile: docker/Dockerfile  # Dockerfile location
5Debug by inspecting the container filesystem

If you're unsure what files exist in the container, inspect directly:

During build:

dockerfile
WORKDIR /app
COPY package*.json ./
RUN ls -la && cat package.json
RUN npm install

In a running container:

bash
# 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.json

Check if volume is hiding files:

bash
# 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 /app
6Fix Windows-specific issues

On 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:

bash
# 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 up

Fix line endings:

bash
# Convert Windows line endings to Unix
git config --global core.autocrlf input

# Or use dos2unix
dos2unix package.json

Advanced notes

Anonymous volumes for node_modules: A common pattern to prevent host node_modules from overwriting container modules can itself cause ENOENT errors:

yaml
volumes:
  - ./:/app
  - /app/node_modules  # Anonymous volume

The 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:

bash
docker-compose down -v  # Remove anonymous volumes
docker-compose up --build

Multi-stage builds: In multi-stage Dockerfiles, ensure you're copying from the correct stage:

dockerfile
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:

dockerfile
COPY package*.json ./
RUN npm ci --only=production

Debugging build context: To see exactly what files Docker receives:

bash
# 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