Create React App fails to build inside docker - node.js

I am trying to dockerize a react application.
It uses craco for building since I am using tailwindcss.
It was working properly until today when the build started throwing errors for CSS files.
The error
> [admin-build 7/7] RUN npm run build:
#15 1.594
#15 1.594 > admin#0.1.0 build /app
#15 1.594 > craco build
#15 1.594
#15 3.555 craco: *** Cannot find ESLint loader (eslint-loader). ***
#15 3.873 Creating an optimized production build...
#15 89.72 Failed to compile.
#15 89.72
#15 89.72 ./src/styles/index.css
#15 89.72 TypeError: Cannot convert undefined or null to object
#15 89.72 at Function.entries (<anonymous>)
#15 89.72 at Array.forEach (<anonymous>)
#15 89.72
#15 89.72
#15 89.75 npm ERR! code ELIFECYCLE
#15 89.75 npm ERR! errno 1
#15 89.76 npm ERR! admin#0.1.0 build: `craco build`
#15 89.76 npm ERR! Exit status 1
#15 89.76 npm ERR!
#15 89.76 npm ERR! Failed at the admin#0.1.0 build script.
#15 89.76 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#15 89.76
#15 89.76 npm ERR! A complete log of this run can be found in:
#15 89.76 npm ERR! /root/.npm/_logs/2021-06-26T14_32_59_262Z-debug.log
------
My Dockerfile
# base image
FROM node:14-alpine as admin-build
# workdir
RUN mkdir /app
WORKDIR /app
# Install dependencies
COPY package.json ./
RUN npm install
# copy source
COPY . .
# build source
RUN npm run build
# Nginx for serving
FROM nginx:alpine
# copy configs
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=admin-build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
The application builds properly outside docker.
Is there any way to fix this or at least see what the problem is?
Thanks

Just add the ENV to Dockerfile,
this one work fine for me:
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

This was an issue with the node version in docker.
I was using node 16 and npm version 7 in my local setup and in docker, it was running node-14 and npm 6.
This seems to be causing problems with craco.
After updating the docker file to use node-16:alpine, It worked.

Related

Docker and NPM private registry

i use jetbrains space-on-permises on my pc. When i try to build a node docker image with a npm registry set in the npm registry i've a error:
this is my docker file:
# Builder stage
FROM node:18 as builder
WORKDIR /usr/src/app
COPY package.json ./
COPY snkrs-url.json ./
COPY .npmrc ./
COPY . .
RUN npm config delete proxy
RUN npm config set #sneakers-monitor:registry http://packages:8390/npm/p/sneakers-monitor/npm/
RUN npm config set //packages:8390/npm/p/sneakers-monitor/npm/:_authToken=eyJhbGciOiJSUzUxMiJ9.eyJzdWIiOiIxbjdENmoxc1Y0ZHQiLCJhdWQiOiJjaXJjbGV0LXdlYi11aSIsIm9yZ0RvbWFpbiI6ImpldGJyYWlucyIsIm5hbWUiOiJhZG1pbiIsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdDo4MDg0IiwicGVybV90b2tlbiI6IjJuWHFuRDRYRTVCTiIsInByaW5jaXBhbF90eXBlIjoiVVNFUiIsImlhdCI6MTY3MDY3Njk1Mn0.g7sOBo7r7SvNuWq7VvKttdusdMf5Vad2Als24xBuGFDBId1oGa6XG61Y6X98mE_lifvtMxydD5DFspe49lH2TyS2hhNUI2vYxMno9jcA3oWGNjXMuK8cpAEterD70aNl_yCT-bS2hTh0jeVUKtLewGvoiXIPOvrCTMnyYpHA_qmixi1Qtw49O5rnB9SzTx6Go_iHUJB1p4ZFhEvY_mH8AVFSTo00YRE-RQEQ0j3p54me9amlC5xUTRg6c3bI5J9ElqVPgKn2arODsITKtYkD3vS6K3MRUtCnrr2eKxw3LtDYH1GfXq-hCw5TlYxWiBB_z21U7tnpOCKM8GkxrH-E3hdlYwt4ychoFveDclQKCC8PsMVOWOI9oaMqVttWMz9HkgvAEB3y--pV54s_pHm6xrH_CrYL2oEZ6rELjyJDaZYB-EPpLOrRRIXOYlitYUpe8nKcz13I7HEChpT0huXiP6IWqdotvtSdnR89irqoDILH7RZbGGjkFYB6Yz04zPwzn91-CBONuTMRLm39iv880l2tU6-7BlM2Jivyx0I_IKJCqJjymE0zfXJqB9I1F1S4T2GHBALHbTXtXa9dgC-unwJN3L_4TEPxaozzzcH8ikld8GOcd3bXJX1d-iD4GjqX9ek8cidwqmRzR3ZobAVKnb7_NC0vrXxG14BTqrvJNBg
RUN npm install
RUN npm run build
RUN npm prune --production
RUN wget https://gobinaries.com/tj/node-prune --output-document - | /bin/sh && node-prune
# Runner stage
FROM node:18-alpine as runner
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/snkrs-url.json ./
COPY --from=builder /usr/src/app/dist ./dist
COPY --from=builder /usr/src/app/node_modules ./node_modules
EXPOSE 5000
CMD [ "node", "dist/index.js" ]
And this is the error:
#18 188.0 npm ERR! code ENOTFOUND
#18 188.0 npm ERR! syscall getaddrinfo
#18 188.0 npm ERR! errno ENOTFOUND
#18 188.0 npm ERR! network request to http://packages:8390/npm/p/sneakers-monitor/npm/#sneakers-monitor%2fdiscord-bot failed, reason: getaddrinfo ENOTFOUND packages
#18 188.0 npm ERR! network This is a problem related to network connectivity.
#18 188.0 npm ERR! network In most cases you are behind a proxy or have bad network settings.
#18 188.0 npm ERR! network
#18 188.0 npm ERR! network If you are behind a proxy, please make sure that the
#18 188.0 npm ERR! network 'proxy' config is set properly. See: 'npm help config'
#18 188.0
Can anyone help me?

unable to run npm install in docker image (getting auth error)?

I have a very simple Dockerfile
FROM node:17.3.1 as build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
FROM node:17.3.1
WORKDIR /app
COPY package.json .
RUN npm install --only=production
COPY --from=build /app/dist ./dist
CMD npm run start:prod
When running docker build -t nestjs-hello-world . I am getting the following error. I don't understand why it is needing to login to npm. It is using the default registry. Even tried specifying the default registry as part of the npm install command, just to make sure, but same error..
=> ERROR [build 4/6] RUN npm install 8.3s
------
> [build 4/6] RUN npm install:
#8 8.230 npm notice
#8 8.231 npm notice New minor version of npm available! 8.1.2 -> 8.3.1
#8 8.231 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.3.1>
#8 8.232 npm notice Run `npm install -g npm#8.3.1` to update!
#8 8.233 npm notice
#8 8.235 npm ERR! code E401
#8 8.237 npm ERR! Unable to authenticate, your authentication token seems to be invalid.
#8 8.238 npm ERR! To correct this please trying logging in again with:
#8 8.239 npm ERR! npm login
#8 8.251
#8 8.252 npm ERR! A complete log of this run can be found in:
#8 8.252 npm ERR! /root/.npm/_logs/2022-01-20T00_08_48_935Z-debug.log
Any ideas why this is happening for me please?
Thanks
Thanks to Phil I had the same problem and I managed to unblock my problem thanks to your comment
Here is my Dockerfile
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
# copying packages first helps take advantage of docker layers
COPY package.json .
RUN npm install prettier -g
# If you are building your code for production
RUN npm install
# Bundle app source
COPY . .
RUN npm run build
EXPOSE 4000
CMD [ "node", "dist/server.js" ]

node-pre-gyp not accessible from fsevents

I am getting the following error when deploying my container
#15 1.066 npm ERR! node-pre-gyp not accessible from fsevents
#15 1.076
#15 1.076 npm ERR! A complete log of this run can be found in:
#15 1.076 npm ERR! /root/.npm/_logs/2022-01-11T11_40_13_120Z-debug.log
------
executor failed running [/bin/sh -c npm ci]: exit code: 1
following is my dockerfile
WORKDIR /app
COPY package*.json ./
COPY .npmrc .npmrc
RUN npm set progress=false
RUN npm ci
RUN npm audit
COPY . .
RUN npm run build
FROM nginx
RUN mkdir /app
COPY --from=0 /app/dist /app
COPY ./__docker_content_start.sh /start.sh
RUN chmod +x /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
CMD /start.sh
I am unable to find a solution for this. Any help appreciated Thanks
Add node-pre-gyp as a dependency to your package.json
npm i --save node-pre-gyp

Dockerfile with angular and nginx fails at the build phase

I'm trying to build a docker image for my project with angular and nginx, but I get the following errors at the build phase when trying to mount the image (install seems to work fine):
#11 103.1 Error: src/app/models/index.ts:5:28 - error TS2307: Cannot find module './inputField' or its corresponding type declarations.
#11 103.1
#11 103.1 5 export { InputField } from './inputField';
#11 103.1 ~~~~~~~~~~~~~~
#11 103.1
#11 103.1
#11 103.1
#11 103.1 npm ERR! code ELIFECYCLE
#11 103.1 npm ERR! errno 1
#11 103.2 npm ERR! myproject#11.0.0 build: `ng build --build-optimizer --output-hashing=none`
#11 103.2 npm ERR! Exit status 1
#11 103.2 npm ERR!
#11 103.2 npm ERR! Failed at the myproject#11.0.0 build script.
#11 103.2 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#11 103.2
#11 103.2 npm ERR! A complete log of this run can be found in:
#11 103.2 npm ERR! /root/.npm/_logs/2021-07-17T10_12_55_066Z-debug.log
------
executor failed running [/bin/sh -c npm run build]: exit code: 1
My dockerfile:
# Angular image
FROM node:latest as build
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# Nginx image
FROM nginx:latest
COPY --from=build /dist/angular/ /usr/share/nginx/html/
EXPOSE 80
I've done npm audit fix to solve a couple issues and I've deleted node_modules folder, cache and package-lock.json, and installed again, but none of that seems to help.
Thanks in advance.
According to the original issue:
error TS2307: Cannot find module './inputField'
Renaming a file from InputField.ts to inputField.ts solved the problem with the build inside a container.
Looking inside Build Container:
Running only a build container was an excellent comment from #David Maze
something like this:
$ docker run --rm -it <name_of_the_container> /bin/bash
this way you can issue a npm run build command and see what is happening inside.
What I also recommend is to have a clean ng new ngApp as a reference.
If that app can be built then something else is the problem and not the Dockerfile.
Sample:
# Angular build image
FROM node:latest as builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# Nginx image
FROM nginx:latest
COPY --from=builder /app/dist/ngApp /usr/share/nginx/html/
EXPOSE 80
Try running ng build command locally before building the docker image. It will show the errors. Currently it can't access the InputField.
export { InputField } from './inputField';

How to build a production only angular 9 docker image

I am not much into frontend development. I am building a docker image for the angular 9 application. Here is something I tried.
ARG NODE_VERSION=12.16.1
# stage - # 1
FROM node:${NODE_VERSION}-buster-slim as builder
WORKDIR /app
COPY . .
RUN npm install --only=production && npm run build --prod
# stage - #final
FROM nginx:stable
COPY --from=builder /webapp/dist/webapp /usr/share/nginx/html
COPY demo-nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
I am getting the following error.
#10 5.883 > webapp#0.0.0 build /webapp
#10 5.883 > ng build
#10 5.883
#10 5.885 sh: 1: ng: not found
#10 5.887 npm ERR! code ELIFECYCLE
#10 5.887 npm ERR! syscall spawn
#10 5.887 npm ERR! file sh
#10 5.887 npm ERR! errno ENOENT
#10 5.888 npm ERR! webapp#0.0.0 build: `ng build`
#10 5.888 npm ERR! spawn ENOENT
#10 5.888 npm ERR!
#10 5.888 npm ERR! Failed at the webapp#0.0.0 build script.
#10 5.888 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#10 5.892
when I do the production only npm install the "ng" command is not being installed to the first phase of the image. If I do not use with the --only=production then all the dev dependencies are installed along with it.
How can I only get the production only dependencies, yet build it for production ?
Does the npm run build --prod command create a /dist folder only containing the production files ?
How to do it correctly ?
This is working file :
ARG NODE_VERSION=12.16.1
# stage - # 1
FROM node:${NODE_VERSION}-buster-slim as builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod
# stage - #final
FROM nginx:stable
COPY --from=builder /app/dist/{add_angularapp_name} /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]

Resources