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.

How to handle `cannot find module`

See original GitHub issue

I am using string literals to dynamically import components based off text entered into my CMS. In the case that the user enters text for a component that does not exist, id love to be able to fallback.

In the below code i have tried using an ErrorBoundary, fallback of null, and a try/catch block, but i still get the ‘Error: Cannot find module’ error.

const ActualIcon = ({ icon }) => {
	try {
		const Icon = loadable(() => import(`../Icons/elements/${icon}`), {
			fallback: null,
		});
		return <Icon />;
	} catch (e) {
		return null;
	}
};

const FeatureIcon = ({ icon }) => (
	<ErrorBoundary>
		<ActualIcon icon={icon} />
	</ErrorBoundary>
);

Is there some other way i can prevent this error when trying to use loadable on a component that doesnt exist?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
haxxxtoncommented, Sep 25, 2019

@foglerek thanks for your help. The solution appears to be:

import(`../${dynamicValue}`)
  .catch(() => import('../fallbackComponent'));

otherwise the fallbackComponent is always rendered even if there is a successful import 😃

0reactions
iamdcjcommented, Mar 30, 2020

Hey @haxxxton,

I’m having the same issue, did you manage to find a solid solution? If so, could you please share.

Thanks, David

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I resolve "Cannot find module" error using Node.js?
This error can be encountered if you are require ing a module that has a missing or incorrect main ...
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
To solve the "Cannot find module" error in Node.js, make sure to install the package from the error message if it's a third-party...
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 >
How to fix npm cannot find module error - Nathan Sebhastian
The cannot find module error occurs because npm cannot find the module required by the index.js file. In this case, the axios module....
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