‘ENOENT: No such file or directory’ Error in Docker build

Solution for ‘ENOENT: No such file or directory’ Error in Docker build
is Given Below:

I was trying to run the following code in the terminal:

docker build -t node-docker-project .

My ‘Dockerfile‘ is as follows:

FROM node:16-slim
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD ["npm", "start"]

I tried severally, but the error that is brought out continuously is as shown below:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /app/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

I thought that I had not installed dependencies but I have checked that out and it is all well in my local app directory. What is the problem?

Solution

The problem of the code above is as indicated in the error message. It states that it can’t find the file in the specified directory:

ENOENT: no such file or directory, open '/app/package.json'

Look at the ‘Dockerfile‘, you will see that you have used ./app in the following:

COPY package.json ./app

instead of /app (Remove the . ) because it tries to navigate into an unsearchable folder and file. Run it and now it shall give you a success message.