Npm ERR! enoent ENOENT: no such file or directory, open '/usr/app/package.json' CI error but works locally

as you see i’m already wrote COPY package*.json /usr/app/
when i’m try to run docker-compose run bona_api ls it only returns node_modules

Dockerfile:

FROM node:16.3.0-alpine
WORKDIR /usr/app
COPY package*.json /usr/app/
COPY ecosystem.config.js .
# COPY run.sh /
RUN npm i
RUN npm install -g pg
RUN npm install -g typescript@latest
RUN npm install -g nodemon
RUN npm install pm2 -g
COPY . .
EXPOSE 3000
ARG STAGE
ARG START_SCRIPT
RUN echo "will srart on stage ${STAGE}"

RUN chmod -R 777 /root

CMD ["sh", "-c", "npm run ${START_SCRIPT}"]

config.yml:

version: 2
orbs:
  shellcheck: circleci/shellcheck@x.y.z
jobs:
  build:
    working_directory: /backend
    docker:
      - image: docker:17.05.0-ce-git
    steps:
      - checkout
      - run:
          name: "Pull Submodules"
          command: |
            git submodule sync
            git submodule update --init
      - restore_cache:
          key: v1-deps-{{ checksum "package-lock.json" }}
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: Install dependencies
          command: |
            apk add --no-cache py-pip=9.0.0-r1
            pip install docker-compose==1.15.0
            cp .example.env .env
      - save_cache:
          key: v1-deps-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - run:
          name: start service
          command: |
            cp .example.env .env
            apk update && apk add curl curl-dev bash
            docker network create bona-invest_default || true
            docker-compose up --build --remove-orphans
      - run:
          name: run tests
          command: |
            chmod 777 ./tests_start.sh
            ./tests_start.sh
      - store_artifacts:
          path: test-reports/
          destination: bona-backend

what do you see if you do

docker exec -it -u root bona_api ls

This is not a solution, but more discovery to understand what may be going on

Another thing to try would be to add the following to the Dockerfile after the ‘COPY . .’

RUN ls
RUN ls /usr/app

At least that way you will know what is going on during the build at the point that the files should be in place - if they are not it will be worth building with the --no-cache flag and seeing what happens.

the result of the command docker exec -it -u root bona_api ls is Error response from daemon: Container 9d0273e2e9cfe6ca7ae8a8d4015cc4aa96ef3e8fee94893f0d189a6479d3aee6 is restarting, wait until the container is running even if i run sleep command after the service started

any ls command from inside the dockerfile works as expected with all the files in the project

OK, the first answer is more an indication of the docker versions I use and the command structures I use to using than anything your end.

If the files are showing up correctly within the docker container during its build, I have to ask do you have a docker-compose.yml file setup that is changing the user that the container is run under? You are most likely to be building using root.

yes, i’m actually don’t defining any other user
here you are my docker-compose.yml:

version: '3.7'
services:
  bona_api:
    container_name: bona_api
    restart: always
    build: .
    volumes:
      - .:/usr/app/
      - node_modules:/usr/app/node_modules
    ports:
      - "${DOCKER_PORT}:3000"
    networks:
      - bona_db
    external_links:
      - bona-pg:bona-pg
      - bona-pg-test:bona-pg-test
      - bona-cache:bona-cache
    environment:
      - STAGE=${STAGE}
      - START_SCRIPT=${START_SCRIPT}
volumes:
 node_modules:

networks:
  bona_db:
    external:
      name: bona-invest_default