仅在存在模块的情况下如何要求模块。反应本机 [英] How to require module only if exist. React native

查看:160
本文介绍了仅在存在模块的情况下如何要求模块。反应本机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

  let tmp; 

试试{
tmp = require(‘module-name’);
} catch(e){
回报;
}

我收到错误消息(反应了本地的Metro Bundler):

 错误:捆绑失败:错误:无法从... 
<中解析模块`module-name` / pre>

仅在存在时如何要求模块名?

解决方案

这对我有用:

  let myPackage; 
const myPackageToRequire =‘my-package-to-require’;
try {
myPackage = require.call(null,myPackageToRequire);
} catch(e){}

变量定义 const myPackageToRequire ='my-package-to-require'; 在这里是必需的。



希望我有所帮助。


Example:

let tmp;

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

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 = require.call(null, myPackageToRequire);
} catch (e) {}

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

Hope I helped.

这篇关于仅在存在模块的情况下如何要求模块。反应本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆