When building a react app on production getting Error Module not found: Error: Can't resolve 'fs' in '/usr/src/app/node_modules/jpeg-exif/lib'
Node Version
Node version 18
Running command in Docker Production
npm install --legacy-peer-deps
npm run build
package.json file
"name": "dcts-client",
"version": "0.1.0",
"private": true,
"homepage": "ecommerce",
"dependencies": {
"@emotion/react": "^11.10.0",
"@emotion/styled": "^11.10.0",
"@fortawesome/fontawesome-svg-core": "^6.1.2",
"@fortawesome/free-brands-svg-icons": "^6.1.2",
"@fortawesome/free-solid-svg-icons": "^6.1.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@hookform/error-message": "^2.0.0",
"@mui/icons-material": "^5.11.0",
"@mui/material": "^5.10.2",
"@mui/x-date-pickers": "^5.0.0",
"@react-pdf/renderer": "^3.0.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bootstrap": "^5.2.0",
"card-validator": "^8.1.1",
"cross-domain-storage": "^2.0.7",
"dayjs": "^1.11.5",
"env-cmd": "^10.1.0",
"js-cookie": "^3.0.1",
"lodash.isequal": "^4.5.0",
"payment": "^2.4.6",
"query-string": "^7.1.1",
"react": "^18.2.0",
"react-bootstrap": "^2.5.0",
"react-confirm-alert": "^3.0.6",
"react-credit-cards": "^0.8.3",
"react-csv": "^2.2.2",
"react-data-table-component": "^7.5.3",
"react-datepicker": "^4.8.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.36.1",
"react-input-range": "^1.3.0",
"react-medium-image-zoom": "^5.0.2",
"react-phone-input-2": "^2.15.1",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"react-select": "^5.5.9",
"react-slick": "^0.29.0",
"react-to-print": "^2.14.8",
"react-toastify": "^9.0.8",
"simplebar-react": "^2.4.1",
"slick-carousel": "^1.8.1",
"styled-components": "^5.3.6",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "set PORT=3000 && react-scripts start",
"build": "react-scripts build",
"build:production": "set REACT_APP_API_ENDPOINT_BASE_URL=/api && set REACT_APP_API_ENDPOINT_REDIRECT_URL=http://alb-dct-prd-1561780064.us-east-2.elb.amazonaws.com/ && react-scripts build",
"build:testing": "export REACT_APP_API_ENDPOINT_BASE_URL=/api REACT_APP_API_ENDPOINT_REDIRECT_URL=http://dctalb-1569668697.us-east-2.elb.amazonaws.com/ && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
docker File
WORKDIR /usr/src/app/ecommerce
ARG PAYTRACE_URL
ARG REDIRECT_URL
COPY ./DCT-ECOMMERCE/package.json ./
RUN npm install --legacy-peer-deps
COPY ./DCT-ECOMMERCE .
RUN sed -i "s/protect.sandbox.paytrace.com/${PAYTRACE_URL}/g" /usr/src/app/ecommerce/public/index.html
RUN export REACT_APP_API_ENDPOINT_BASE_URL=/ecomm-api REACT_APP_API_ENDPOINT_REDIRECT_URL=${REDIRECT_URL} && npm run build -- --profile
FROM nginx:1.24.0-alpine
COPY ./ngnix/nginx-default.conf.template /etc/nginx/conf.d/default.conf.template
COPY --from=build-ecommerce /usr/src/app/ecommerce/build/ /usr/share/nginx/html/ecommerce
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80
Unexpected behavior
3
Answers
Based on the error message, "Module not found: Error: Can’t resolve ‘fs’ in ‘/usr/src/app/node_modules/jpeg-exif/lib’", and your provided package.json, it seems you have a FE project designed for browser environments. The error indicates that jpeg-exif is trying to use Node.js‘s
fs
module, which isn’t available in browsers.To resolve this, consider adding a
browser: { fs: false }
setting in your package.json. This tells the bundler (like Webpack or Browserify) to ignorefs
module references for browser builds. Add this to your package.json:Alternatively, if you’re using a build tool like Webpack in react-scripts, modify its configuration:
After these changes, rebuild your project to see if the issue is resolved.
I have the same issue, for me its because @react-pdf/renderer
So i’m uninstalling @react-pdf/renderer, and then add again manually
in package.json
then go to package-lock.json and add this
then on terminal
I think is because of inside your
@react-pdf/renderer
package.jsondependencies is using
^x.x.x
in@react-pdf/pdfkit
and@react-pdf/image
(You can check by package-lock.json), so the version is not same as before.Try add
"@react-pdf/pdfkit": "3.0.0"
and"@react-pdf/image": "2.3.0"
in your package.json and remove node_modules to reinstall. (You might check the version of@react-pdf/image
and@react-pdf/pdfkit
have nojpeg-exif
in their package.json)And use
npm install --legacy-peer-deps
might solve your problem.