How to require module only if exist. React native

Related searches


let tmp;

try {
  tmp = require('module-name');
} catch(e) {

I get error (react native Metro Bundler):

error: bundling failed: Error: Unable to resolve module `module-name` from ...

How to require "module-name" only if exist?

That's what works for me:

let myPackage;
const myPackageToRequire = 'my-package-to-require';
try {
  myPackage =, myPackageToRequire);
} catch (e) {}

The variable definition const myPackageToRequire = 'my-package-to-require'; is necessary here.

Hope I helped.

Require file dynamically by variable � Issue #6391 � facebook/react , I appreciate your help, but I'd like to know if there is any way to require file with dynamic + '\''); } else { console.warn('Requiring module \'' + verboseName + '\' by name is only Fixes for latest react-native / metro-bundler realm/realm-js# 1332. If React Native doesn't support a native feature that you need, you should be able to build it yourself. This is a more advanced guide that shows how to build a native module. It assumes the reader knows Objective-C or Swift and core libraries (Foundation, UIKit).

Loading optional dependencies via try-catch has been added in Metro 0.59, which in turn means that you should be able to use your original code in React Native 0.63 if you turn it on in metro.config.js:

module.exports = {
  transformer: {
    allowOptionalDependencies: true,

Error: Unable to resolve module `./index` from `\node_modules\react , index could not be found from \node_modules\react-native\scripts/. Indeed, none of these files exist: #23908 the issue only happens when I press Play button and build using xcode, but when I run npm start -- --reset-cache Load more… If React Native doesn't support a native feature that you need, you should be able to build it yourself. Native Module Setup. Native modules are usually distributed as npm packages, apart from the typical javascript files and resources they will contain an Android library project.

Use require.resolve which will return resolved file name.

function checkModuleAvailability (module) {
  try {
    return true
  } catch(e) {
    console.log(`${module} not found`);
  return false

const moduleAvailable = checkModuleAvailability(MODULE_NAME) // true or false

ReactDOM – React, If you use ES5 with npm, you can write var ReactDOM = require('react-dom') . Most of your components should not need to use this module. ReactDOM. render() does not modify the container node (only modifies the children of the container). into the DOM, this returns the corresponding native browser DOM element. From what I see, you are trying to require and pass the Cyton module from react-native to nodejs-mobile through the bridge, but the bridge is only built for passing strings. My advice would be to require and execute all Cyton related operations on nodejs-mobile side, as it seems to be a module that requires fs , and using the bridge as a way to

Test Renderer – React, Essentially, this package makes it easy to grab a snapshot of the platform view by a React DOM or React Native component without using a browser or jsdom. app.js'; // The component being tested // render the component let root; act(() unless you're writing your own assertion library on top of the test renderer. Partial solution through the following: Upgraded to RN 0.41; Installed react-native-maps v13.0. The standard "npm install react-native-maps --save" in the installation guide was installing an earlier release.

Dynamic imports, That's possible only because the structure of imports/exports is simple and fixed. But how let modulePath = prompt("Which module to load? For instance, if we have the following module say.js : If my module exists only if my platform is Android (if it is web the module does not exist in package.json and� Metro server instance here is started by runAndroid.js if you ask for react-native run-android I can help you with android only, I am giving you fix on windows, if you are on Linux then I am sure you definitely know how to apply these fixes on your machine.

If you go out to./node_modules/react-native/Libraries/react-native/react-native-implementation.js you should see a line like const warnOnce = require ('warnOnce'); on line 14 This line is importing the warnOnce module using haste. The warnOnce file should be in./node_modules/react-native/Libraries/Utilities/warnOnce.js (confirm it's there)

  • Try require.resolve(PACKAGE_NAME)
  • @PritishVaidya not working(( Same error
  • When I am testing this with a module I absolutely have in my node_modules (inappbrowser-reborn), I am getting: Unknown named module: "react-native-inappbrowser-reborn".