0

I have a react app where I use absolute imports instead of relative imports (https://medium.com/@ktruong008/absolute-imports-with-create-react-app-4338fbca7e3d). Everything works as should on my local machine but when I try to build on docker it fails.

I have an .env file which contains NODE_PATH='./' and create-react-app is configured in such a way that its webpack configuration will automatically pick up ‘.env’ files and read the NODE_PATH environment variable, which can then be used for absolute imports so something like src/components/NavBar, resolves.

But when I try to build on docker, it doesn't resolve but throws this error.

Cannot find module: 'src/components/NavBar'. Make sure this package is installed.
You can install this package by running: yarn add src/components/NavBar.

Any pointers would be appreciated.

Today is my first time working with Docker so I’m a noob.

Additional Info:

I use a Dockerfile and a docker-compose.yml file.

I use react-app-rewired (https://github.com/timarney/react-app-rewired) which basically helps you override create-react-app webpack configs without ejecting.

Dockerfile

FROM node:10.15.1

ENV NODE_ENV production

RUN mkdir /usr/invoicing

COPY . /usr/invoicing

WORKDIR /usr/invoicing

RUN npm install

RUN npm run build

docker-compose.yml

version: '3.5'
services:
  web:
    container_name: invoice-frontend
    build:
      context: .
      dockerfile: Dockerfile
    command: npm start
    ports:
      - '3000:3000'
    networks:
      - frontend
    environment:
      - NODE_ENV=production
networks:
  frontend:
    driver: 'bridge'
Chiamaka Nwolisa
  • 951
  • 8
  • 15
  • 1
    Could you please provide both `docker-compose.yml` and `Dockerfile`? – Carlos Frias Jan 31 '19 at 19:07
  • 1
    replace your relative path `NODE_PATH='./'` with a real absolute full path as viewed from inside the container ... keep in mind when launching a container its yaml volumes map full path on parent host machine to full path as viewed from inside the container ... its helpful to launch a container which does nothing just so you have something running which you can login to and view volumes inside ... details see https://stackoverflow.com/a/31879013/147175 – Scott Stensland Jan 31 '19 at 19:41
  • @ScottStensland Thanks for the pointer; I used `docker exec -it (name-of-image)` and ran pwd to get the working directory and swapped the value in my NODE_PATH but still, doesn't resolve – Chiamaka Nwolisa Jan 31 '19 at 22:46
  • @CarlosFrias please i've updated my post with docker-compose.yml and dockerfile – Chiamaka Nwolisa Jan 31 '19 at 22:49
  • I used your configuration but I wasn't able to replicate the issue, maybe you're using an old built image, try running `docker-compose up --build`, to force a rebuild of the image from the `Dockerfile`, tell me if that works – Carlos Frias Jan 31 '19 at 23:46

1 Answers1

9

I’ve found the problem and the solution:

Apparently, I named a folder navBar and when I renamed it to be NavBar, Git didn't detect a change in the foldername and didn't do anything about it. So on my local machine (which is a Mac), it shows NavBar but on Gitlab which uses git commits, it still appeared as navBar and cos Docker runs on Linux and Linux is case sensitive, the build fails.

This also explains why the build worked on my local machine. Mac’s filesystem hfplus is case insensitive so it didn't see a problem.

To solve the renaming problem; I used the git mv command. There’s a caveat tho; if you want to rename a foldername to folderName (ie change the case) on case insensitive file systems like Mac, you’ll get an error saying fatal: renaming ‘foldername’ failed: Invalid argument.

In order to rename the folder successfully, use this command. git mv foldername tempname && git mv tempname folderName. This splits the process into two steps; first renaming the foldername to tempname and the renaming tempname to folderName.

Hopefully, this helps someone

Chiamaka Nwolisa
  • 951
  • 8
  • 15
  • 1
    ran into the same issue today and this post saved me. – oshell Sep 20 '19 at 13:18
  • 1
    Got the same issue - my local machine is Mac, and my CI/CD pipeline works on Linux. Spent around 2 days to solving the problem before I found your answer. Thanks a lot! – chuve Feb 28 '21 at 08:21