question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught Error: Cannot find module

See original GitHub issue

💬 Questions and Help

First off, thanks for the great library.

I am currently building a server-side rendered application, using Loadable Components (5.12.0) for ‘component-level’ code-splitting. The application contains a componentSwitch component which consumes an array of objects provided by a CMS - the component iterates over this array and returns a <Component /> for each object using an id key to target the relevant directory

const Component: LoadableComponent<any> = loadable(() =>
  import(`../presentation/${id}`)
)

return (
  <ErrorBoundary key={guid}>
    <Component {...data} />
  </ErrorBoundary>
)

This works just fine when the IDs returned from the server match-up to components in the application, but if the CMS returns an ID which doesn’t have a matching component present in the application I get the following error;

Uncaught Error: Cannot find module './SomeComponent'

Initially I wrapped the Loadable import in a try/catch, but Loadable doesn’t throw an error if the imported module cannot be found, instead it returns the following;

// console.log(SomeComponent) 
$$typeof: Symbol(react.forward_ref)
render: ƒ (props, ref)
preload: ƒ (props)
load: ƒ (props)

I attempted to hack around this by running the load method on the returned Loadable before returning the <Component /> from the switcher:

try {
  Component.load();
} catch (error) {
  return null;
}

This hack gets shut of the original error(s), but results in a server/client mismatch warning;

Warning: Expected server HTML to contain a matching <main> in <div>

Right now I am building an array[str] of IDs for each component present in the directory, e.g. ['Hero', 'Statement'] - I then check if the incoming ID is present in the array, if so create a Loadable, else return null. I am satisfied with this approach, however I would much prefer to check if the module exists in a more ‘dynamic’ fashion.

Any guidance will be very much appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

5reactions
stale[bot]commented, May 30, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

2reactions
iamdcjcommented, Aug 17, 2020

I appreciate the reopening of the ticket, but it feels like my opening comment has been lost/ignored.

fallback to ErrorComponent?

I’m not sure if ‘ErrorComponent’ refers to a boundary, but boundaries do not catch on the server-side. If it was a client-side only solution this report wouldn’t exist.

be able to “try” file first (like Component.load()😉

My opening comment touches on this scenario;

I attempted to hack around this by running the load method on the returned Loadable before returning the <Component /> from the switcher:

try {
  Component.load();
} catch (error) {
  return null;
}

This hack gets shut of the original error(s), but results in a server/client mismatch warning;

Warning: Expected server HTML to contain a matching <main> in <div>

I liked this approach; it allowed me to dynamically check a component existed in the codebase without maintaining an array of component IDs, however the server/client mismatch could not be ignored.

I am no longer engineering the application which prompted me to open this ticket, but others seem to be having similar issues. I can only really comment on what I wanted at the time, and that was the ability to have a component switch which loads a component using an CMS-generated, API-provided key(id), for example;

const someComponent = loadable(() => import(…/presentation/${id})

If the API provided key is used to load a component from a directory which does not exist, then it is caught on the server and we simply return null.

It wasn’t a hardship for me to maintain an array of known components, e.g. ['Hero', 'Statement']- this solution allowed me to check that the API provided key matches-up to an existing component in the application, and thus prevent any of the reported issues.

I didn’t have the time to fork and figure out a solution back when I reported the issue, and I don’t see me using loadable-components on the server any time soon. I will close this again, and let others report their specific issue.

Thanks again for the great work on Loadable Components, it is really appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I resolve "Cannot find module" error using Node.js?
This happens when a first npm install has crashed for some reason (SIGINT of npm), or that the delay was too long, or...
Read more >
cannot find module [Node npm Error Solved] - freeCodeCamp
How to Fix the "cannot find module" Error · delete the node modules folder by running rm -rf node_modules · delete package.lock.json file...
Read more >
How to resolve "Cannot find module" error in Node - Sabe.io
The issue is that Node is unable to find the module that you are trying to import into your Node application. The most...
Read more >
Cannot find module 'X' error in Node.js | bobbyhadz
If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed...
Read more >
How to resolve can't find module error in Node.js - Reactgo
To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found