简体   繁体   中英

Docker and Node: ENOENT: no such file or directory, open "package.json"

I have the files

docker-compose.yml

...
  example:
    build: 
      context: ./example
    ports:
      - 3300:3000
    networks:
      - backend
    volumes:
      - ../example/:/var/www
    container_name: example
...

./example/Dockerfile

FROM node:16

WORKDIR /var/www

RUN npm install -g react-scripts

RUN npm install

EXPOSE 3000

CMD [ "npm", "start" ]

When I run

docker-compose build

Get this error

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

But if I comment on the #RUN npm install , the script sees package.json and "npm start" working, but I get error, because there are no node_modules

Where are the contents of the mounted folder, when "npm install"?

Thanks to David Maze , he gave food for the mind.

After several decisions, the best thing for me is the following

docker-compose.yml

...
  example:
    build: 
      context: ./example
    ports:
      - 3300:3000
    volumes:
      - ./example/:/var/www
    container_name: example
...

./example/Dockerfile

FROM node:16

WORKDIR /var/www

RUN npm install -g react-scripts

RUN chown -Rh node:node /var/www

USER node

EXPOSE 3000

CMD [ "sh", "-c", "npm install && npm run start" ]

This solution is great for development. The user node will help you with the rights of host<->guest

The folder node_modules will be accessible from the host and synchronize host<->guest

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