简体   繁体   中英

npm ENOENT: no such file or directory, open '/usr/app/package.json'. While trying to share local space with docker using docker-compose

I am trying to share my local space with my docker container with Volume. BTW I am working on a nest app. The problem is whenever I am trying to enable the volume feature I am getting some weird error!

api_1  | npm ERR! code ENOENT
api_1  | npm ERR! syscall open
api_1  | npm ERR! path /usr/app/package.json
api_1  | npm ERR! errno -2
api_1  | npm ERR! enoent ENOENT: no such file or directory, open '/usr/app/package.json'
api_1  | npm ERR! enoent This is related to npm not being able to find a file.
api_1  | npm ERR! enoent 
api_1  | 
api_1  | npm ERR! A complete log of this run can be found in:
api_1  | npm ERR!     /root/.npm/_logs/2020-05-24T19_44_36_593Z-debug.log

I am calling this weird because I haven't found any proper explanation about this error. although these links are quite close to my problem prob1 , prob2

My docker file -

# Download base image
FROM node:alpine

# Define Base Directory
WORKDIR /usr/app

# Copy and restore packages
COPY ./package*.json ./
RUN npm install

# Copy all other directories
COPY ./ ./

# Setup base command
CMD [ "npm", "run", "start" ]

and my docker-compose file

version: '3'
services: 
   api:
     build:
        context: .
        dockerfile: Dockerfile.dev
    ports : 
        - "4200:4500"
    volumes:
        - /usr/app/node_modules
        - /:/usr/app

as far as I can understand whenever I tried to use volume means /:/usr/app replacing docker directory with local project directory. But on my project root directory, I have package.json file. Also in my docker file, I have copied package.json file.

can anyone help me with this issue? I am new with docker, I am doing this just for learning purpose.

You should change your service name api to app . The name you pick for your service in docker compose will be your directory name within the container. So the docker engine creates a directory for your app in src/usr/api and puts your package.json in it. But -based on the Dockerfile- it searches for package.json in src/usr/app so throw this err.

When you mount a host path to a path inside docker the file that existed in that path will become inaccessible. You copied the files to a path /usr/app. Then when you run the container you mount an empty host path on top of that. This is why the files are missing. What you should do is copy everything to the bind mount and then mount it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM