Symtom

  1. installed create-react-app as global module
    npm install -g create-react-app
    
  2. removed node_modules directory in project
    rm -rf node_modules
    
  3. tried to reinstall node_modules in project with package.json
    npm install
    
  4. node_modules directory is not appeared in container!!

  5. I set .dockerignore for host volume not include node_modules

Cause

  1. in docker-compose.yaml volumn mount overwrited COPY command
    dockerfile image build --> node_modules created --> mount volume
    
  2. install create-react-app locally, and run npx ``` If you’ve previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.

from https://create-react-app.dev/docs/getting-started/


# Solution
1. mount only src dir
* Dockerfile

COPY package.json /project

WORKDIR /project RUN npm install ENV PATH /project/node_modules/.bin:$PATH

* docker-compose.yml

./project/src:/project/src


2. or install node_modules other location, use NODE_PATH env

RUN mkdir -p /project COPY . /project

RUN ln -s /project/package.json /data/package.json WORKDIR /data RUN npm install ENV NODE_PATH /data/node_modules

WORKDIR /project


3. uninstall globally installed CRA

npm uninstall -g create-react-app npm install create-react-app ```