So I have a create-react-app-ts
app that I would like to Dockerize and host on Zeit Now.
Everything works fine locally, running yarn tsc
and react-scripts-ts build
works great.
Creating the Docker image also works great from the following Dockerfile:
FROM mhart/alpine-node:10.9
WORKDIR /usr/src
ARG REACT_APP_API_ENDPOINT
ARG NODE_ENV
COPY yarn.lock package.json ./
RUN yarn
COPY . .
RUN yarn build && mv build /public
However, when publishing to Now, the build script fails on Typescript compilation, outputting compilation errors for most files in the project.
I am able to reproduce that locally as well if I set ENV NODE_ENV production
in my Dockerfile just above WORKDIR...
.
So it would seem that either Typescript or react-scripts-ts
acts differently when NODE_ENV=production
. I've never encountered this error before, and I don't know how to debug it. Running NODE_ENV=production tsc
or NODE_ENV=production react-scripts-ts build
also works fine locally.
I'm running Typescript v 3.0.1 with the following config:
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "build/dist",
"module": "esnext",
"target": "es6",
"lib": ["es6", "dom", "esnext.asynciterable"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
"exclude": ["node_modules", "build", "scripts", "acceptance-tests", "webpack", "jest", "src/setupTests.ts"]
}
Any advice would be much appreciated! :)
EDIT: Added env var args to the Dockerfile. It was originally left out for the sake of brevity, but it ended up being part of the issue and solution
So I finally found the issue! In my original Dockerfile
, NODE_ENV
was set before yarn install
. This means that for the production build, yarn
would not install devDependencies
, and therefore not any of my @types
libraries. This caused all the compilation errors all over the project.
Moving the definition of NODE_ENV
below/after yarn install
in the Dockerfile
solved the issue.
FROM mhart/alpine-node:10.9
WORKDIR /usr/src
COPY yarn.lock package.json ./
RUN yarn
ARG REACT_APP_API_ENDPOINT
ARG NODE_ENV
COPY . .
RUN yarn build && mv build /public
Note: As far as I know, yarn build
will make sure to remove the devDependencies
again, so don't worry about this bloating your build. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With