Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create-react-app shows an error of "You need to enable JavaScript to run this app."

Tags:

I use express for my API. I have a folder named app and another folder named server. app is the client app, using create-react-app as boilerplate, while server is express.js app for the API.

in the app.js file of the server, I wrote

app.get("*", function (req, res) {
    res.sendFile(path.resolve(__dirname, '../app/build/index.html'));
})

But then when I call any API endpoint, I get

You need to enable JavaScript to run this app.

in the response. I'm confused; what's wrong?

like image 327
Jenny Mok Avatar asked Sep 10 '17 07:09

Jenny Mok


People also ask

How do I fix create React error?

i had this issue. The simple solution is to add latest versions of typescript, react-scripts libs. and then run npm install. it works for me.

Does React work if JavaScript is disabled?

React is a JavaScript framework for the front-end. Which means it executes in the browser. If you disable JavaScript in the browser, React doesn't work anymore.


1 Answers

In the build directory you have more files that just index.html. You also have build/js/main.buildNumber.js and build/css/main.buildNumber.css. So when your frontend makes a request to https://yourdomain.com/css/main.buildNumber.js, it incorrectly returns index.html not main.js.

Instead, you should serve the contents of the build folder statically with express.static

    app.use('/', express.static(__dirname + '/'));

Or you can look into the "serve" node module to host your app. This will work nicely with react-router. npm i -g serve then cd build then serve . --single -p 5000. This will serve your app on port 5000 (http://localhost:5000).

like image 133
tanner burton Avatar answered Sep 17 '22 15:09

tanner burton