Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose fails to start with npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'

I built an image from a docker file to run node and copy in a simple node express app which works perfectly. The image builds I can run a container and bash in and the local files are copied. When I use this image in a docker-compose file along with a mongo image the containers build but the node container fails to run with an error is the package.json is missing.

The error returned is

report-node exited with code 254
report-node | npm ERR! path /usr/src/app/package.json
report-node | npm ERR! code ENOENT
report-node | npm ERR! errno -2
report-node | npm ERR! syscall open
report-node | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
report-node | npm ERR! enoent This is related to npm not being able to find a file.
report-node | npm ERR! enoent
report-node |
report-node | npm ERR! A complete log of this run can be found in:
report-node | npm ERR!     /root/.npm/_logs/2019-02-26T10_55_05_562Z-debug.log
report-node exited with code 254

dockerfile

FROM node
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD [ "npm", "run", "dev"  ]

Docker-compose.yml looks like

version: '3'
services:
  app:
    image: gosmith/node10:latest
    container_name: report-node
    restart: always
    volumes:
      - ./:/usr/src/app
    working_dir: /usr/src/app
    depends_on:
      - mongo
    environment:
      NODE_ENV: development
    ports:
      - 4000:4000
      - 8080:8080
    command: npm run dev && npm run serve
  mongo:
    container_name: mongo
    image: mongo
    expose:
      - 27017
    ports:
      - 27037:27017
    volumes:
      - ./data:/data/db

How has the package.json gone missing? Thanks for your help!

like image 872
Gosmith Avatar asked Feb 26 '19 11:02

Gosmith


People also ask

What is enoent error in NPM?

As you can see, there’s an error with code ENOENT that prevents npm start command from running successfully. The code ENOENT means that npm fails to open a file or directory that’s required for executing the command. The npm start command is used to run the start script in the package.json file.

Why can't I run node 8 from a dockerfile?

This is normal, since the Dockerfile for the node:8 image only contains CMD [ "node" ] and no working directory. If you want to run straight from the image you need to at least add a working_dir: /usr/src/app and an entrypoint to the docker-compose.yml.

What does docker-compose up actually do?

The note underneath implies that simply performing a docker-compose up will start a container in which the dependencies will be installed via an npm install and that the application will be started by a npm start command.

How do I run a docker app from an image?

If you want to run straight from the image you need to at least add a working_dir: /usr/src/app and an entrypoint to the docker-compose.yml. In the entrypoint you'll need to execute a bash script in which you first perform an npm install followed by an npm start.


1 Answers

When your docker-compose.yml says

volumes:
  - ./:/usr/src/app

it hides everything that gets done in the Dockerfile and replaces it with the current directory. If your current directory doesn't have the package.json file (maybe you moved everything Docker-related into a subdirectory) it will cause the error you see.

Personally I would just remove these two lines, develop my application locally (without Docker, using the per-project node_modules directory for isolation), and only build the Docker image when I'm actually ready to deploy it.

like image 185
David Maze Avatar answered Sep 30 '22 04:09

David Maze