Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

React shows 'You need to enable JavaScript to run this app' with JavaScript on

ErrorYou need to enable JavaScript to run this app.

Short answer

Seeing that sentence with JavaScript on means the HTML page loaded but your app did not start, or an API call got the HTML page back. Check which request returned index.html. The usual causes are asset paths that do not match where the build is hosted, and a server catch-all route that answers requests it should not.

  • Applies to React 19, react-scripts 5, Node.js 22, Express 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the JavaScript file and the API route directly. If either one returns the HTML page, that response is the problem.

    curl -s http://localhost:3000/ | grep -o 'src="[^"]*"'
    curl -s -i http://localhost:3000/api/items | head -n 8
  2. 2Match what you see, then jump to its fix

    1. 1The page is blank with the message, and the script request in the network tab returns index.html or 404The build assets load from the wrong base pathFix
    2. 2fetch('/api/...') returns the HTML of index.html; the response body starts with <!DOCTYPE html>A catch-all route answers requests meant for files or the APIFix
    3. 3Works in production, but with npm start the fetch gets the app's HTMLIn development the request is not forwarded to the API serverFix

What does the error look like?

  • You need to enable JavaScript to run this app.
  • fetch('/api/...') returns HTML instead of JSON

Cause 1 of 3

The build assets load from the wrong base path

1Check

A build assumes it is served from the server root unless package.json has a homepage field. Open the built index.html and look at the script and stylesheet URLs.

grep -o 'src="[^"]*"' build/index.html

2Fix

Set homepage to where the app is hosted. A relative value of . makes asset paths relative when you do not use client-side routing. Rebuild afterwards.

// package.json
"homepage": "."

npm run build

3Verify

Serve the build and load a page; the scripts now return JavaScript, not HTML.

npm install -g serve
serve -s build
curl -sI http://localhost:3000/ | head -n 1

Confirmed by create-react-app.dev

Did this fix it?

Cause 2 of 3

A catch-all route answers requests meant for files or the API

1Check

In Express, a wildcard that sends index.html catches every path that no earlier route handled. Routes registered after it never run.

curl -s -i http://localhost:5000/api/items | head -n 12

2Fix

Register static files and API routes first, and put the index.html catch-all last.

app.use(express.static(path.join(__dirname, 'build')));
app.use('/api', apiRouter);
app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

3Verify

The API URL now returns JSON and the browser URL for a client route returns the app.

curl -s -i http://localhost:5000/api/items | head -n 12

Confirmed by create-react-app.dev

Did this fix it?

Cause 3 of 3

In development the request is not forwarded to the API server

1Check

The development server forwards only requests without text/html in their Accept header to the proxy target, and only when proxy is set. Check package.json.

grep -n '"proxy"' package.json

2Fix

Set proxy to your API server, restart npm start, and call the API with fetch so the Accept header is not text/html.

// package.json
"proxy": "http://localhost:4000"

3Verify

The request in the network tab reaches the API and returns JSON.

npm start

Confirmed by create-react-app.dev

Did this fix it?

How do I stop it coming back?

  • Keep static and API routes ahead of the index.html catch-all in every server.
  • Test the production build with a static server before deploying, not only npm start.
  • Use full or env-driven API base URLs so a wrong path fails with a clear 404.

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:

Related fixes

Back end

The AWS SDK in Node.js cannot find any credentials

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.