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'