P粉0314920812023-08-29 09:09:08
The difference is how the JavaScript code is executed in the browser. When you serve a React application through a webpack development server, express server, or any other type of server, the JavaScript code is executed in the context of the web page, with access to the DOM and all of the Web API. However, when you open an HTML file statically, the JavaScript code is executed in an
isolated environment, and access to the Web API is limited. This is why React code that relies on DOM and Web API doesn't work when opened manually.
Webpack, vite and express provide a dynamic environment with all the necessary APIs and services required for React to run. This is done by serving the application as a web page over HTTP, which is then loaded and executed by the browser.