How to require module only if exist. React native

前端 未结 3 469
广开言路
广开言路 2020-12-11 20:34

Example:

let tmp;

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

I get error (react native Metro Bundler):



        
3条回答
  •  一个人的身影
    2020-12-11 21:03

    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,
      },
    }
    

提交回复
热议问题