I am using Vite, React, React Router and TypeScript for my website. I have a problem when running the production build, in dev mode everything works fine.
When using the production build my browser shows a white background and I get the following error in my browser console:
The upper link brings me to the following (minified?) source code in line 70:
So maybe it has something to do with React Router?
But I am unsure what exactly causes this error. Therefore, I will try to provide as much relevant information as I can below.
My results when running npm run build
:
package.json:
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"start": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"@faker-js/faker": "^8.0.2",
"@vitejs/plugin-react": "^4.0.3",
"axios": "^1.4.0",
"chart.js": "^4.3.0",
"daisyui": "^3.1.7",
"dayjs": "^1.11.9",
"i18next": "^23.2.7",
"i18next-browser-languagedetector": "^7.1.0",
"lucide-react": "^0.258.0",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.0.1",
"react-router-dom": "^6.14.2",
"vite": "^4.4.6"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
"@types/axios": "^0.14.0",
"@types/node": "^20.1.1",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"autoprefixer": "^10.4.14",
"eslint": "^8.2.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-react-refresh": "^0.3.4",
"jest": "^29.5.0",
"postcss": "^8.4.23",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.2.8",
"tailwindcss": "^3.3.2",
"typescript": "^5.0.2"
}
}
vite.config.ts:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});
tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
tsconfig.node.json:
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
When using npm start
everything works as expected, but if I use npm run build
and then npm run preview
I get the error mentioned above and my browser shows a white background.
I already tried to disable code splitting in Vite by using the following vite.config.ts:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
output: {
manualChunks: {},
},
},
},
});
This did not have any effect however.
I tried replacing out all my fragments in React:
<>
...
</>
with:
<Fragment>
...
<Fragment/>
But this also did not have any effect.
I found the problem.
@DrewReese pointed me in the right direction and I actually took a good look at the minified code.
I realized that the code above was about date formatting and that I use a library called Day.js.
I fixed the problem by changing my imports from:
import * as dayjs from "dayjs";
import * as customParseFormat from "dayjs/plugin/customParseFormat";
to:
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
Side note:
Day.js actually seems to recommend the first way of importing their library for my setup which breaks in the production build.