'How to solve the error in package.json when creating an image?

In the step 4/6 is showing up an error when I try to create this image in Docker:

FROM node:latest

RUN mkdir -p /app/src

WORKDIR /app/src

COPY package.json .

RUN npm install
#IT WILL COPY THE ENTIRE DIR FORECAST TO /app/src INSIDE DOCKER
COPY . .

EXPOSE 3000

CMD {"npm",  "start"}

the error that shows is:

=> ERROR [4/6] COPY package.json .                                                                                0.0s
------
 > [4/6] COPY package.json .:
------
failed to compute cache key: "/package.json" not found: not found


Solution 1:[1]

I was getting a similar error.

failed to compute cache key: "/package.json" not found: not found

For me I included a .dockerignore file and placed many other entries other than node_modules

**/node_modules
README.md
package.json
package-lock.json
docker-compose.dev.yml

So naturally, its not copying required package.json file, and hence the error. Silly mistake from my side, hope this helps someone.

Solution 2:[2]

You can try:

COPY ["package*.json","./"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VivekDev
Solution 2 Evgeni Lilov