React.lazy "Failed to import dynamically imported module"

Viewed 2831

I just started trying to implement lazy loading in my React app. I was learning about React.lazy and Suspense for lazy loading.

The following is the code example in the docs:

import React, { Suspense } from 'react';

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <OtherComponent />
      </Suspense>
    </div>
  );
}

And then my code:

import React, { lazy, Suspense } from 'react';

const Home = lazy(() => import('./containers/home/home.component'));

const App = () => {
    return (
        <div>
            <Suspense fallback={<div>...Loading</div>}>
                <Home />
            </Suspense>
        </div>
    );
};

export default App;

For some reason, my code is giving the following errors:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.


react-dom.development.js:11865 Uncaught TypeError: Failed to fetch dynamically imported module: http://localhost:3000/static/js/containers/home/home


The above error occurred in one of your React components:
    in Unknown (at App.js:38)
    in Suspense (at App.js:37)
    in div (at App.js:35)
    in App (created by ConnectFunction)
    in ConnectFunction (at src/index.js:14)
    in Router (created by BrowserRouter)
    in BrowserRouter (at src/index.js:13)
    in Provider (at src/index.js:12)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://**linkNotAllowedByStackOverFlow**/react-error-boundaries to learn more about error boundaries.

I'm not sure what I'm doing wrong here, and would love to hear from anyone who has experienced something similar

1 Answers

Not sure of the exact cause of the issue, but it seems that some package install was corrupted, deleting node_modules and re-installing resolved the issue.

Related