A Create React App build loads blank because the asset paths are wrong
Can't build create-react-app project with custom PUBLIC_URLShort answer
A Create React App build assumes it is served from the server root. Deployed under a subpath, its script and CSS links point at the wrong place and the page stays blank. Set the homepage field in package.json, or PUBLIC_URL for a full address, rebuild, and give the router the same base.
Fix it fast
1Find out which cause you have
Open the built index.html and look at the paths it uses for scripts and styles.
grep -oE '(src|href)="[^"]+"' build/index.html2Match what you see, then jump to its fix
- 1
index.html has src="/static/js/..." but the app lives at /my-app/homepage is not set for a subpathFix - 2
Assets load from the wrong host or path after exporting PUBLIC_URLPUBLIC_URL is set with or without a host and not rebuiltFix - 3
The page loads but every route shows a 404 or nothingThe router does not know the subpathFix
- 1
What does the error look like?
Can't build create-react-app project with custom PUBLIC_URLBlank page after npm run build when the app is served from a subfolderScript and CSS files under /static/ are requested from the wrong path
Cause 1 of 3
homepage is not set for a subpath
1Check
By default the build assumes the app is hosted at the server root. The homepage field tells it the real root.
grep -n homepage package.json2Fix
Set homepage to the final address, or to a dot to make paths relative when you do not use history-based routing. Rebuild.
// package.json
"homepage": "https://example.com/my-app",
// or relative paths:
"homepage": ".",3Verify
The built index.html now prefixes its paths.
npm run build
grep -oE '(src|href)=\"[^\"]+\"' build/index.htmlConfirmed by create-react-app.dev
Cause 2 of 3
PUBLIC_URL is set with or without a host and not rebuilt
1Check
PUBLIC_URL is used in development and production. Create React App normally ignores the hostname; PUBLIC_URL forces assets to be referenced exactly as given, hostname included.
echo $PUBLIC_URL; grep -n PUBLIC_URL .env* 2>/dev/null2Fix
Set it to the full address you want in the output and run the build again in the same shell.
PUBLIC_URL=https://cdn.example.com/my-app npm run build3Verify
The asset links in the built index.html begin with that address.
grep -oE '(src|href)="[^"]+"' build/index.htmlConfirmed by create-react-app.dev
Cause 3 of 3
The router does not know the subpath
1Check
With client-side routing under a subpath the router needs the same base. The Create React App guide uses the basename prop of BrowserRouter.
grep -rn "BrowserRouter" src/2Fix
Give the router the subpath.
<BrowserRouter basename="/my-app">3Verify
Open a nested route directly.
curl -I https://example.com/my-app/Confirmed by create-react-app.dev
How do I stop it coming back?
- Set homepage once in package.json and treat it as part of the deployment.
- Open the built index.html after every build when the target is a subpath.
- Keep the router base and homepage in step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix