You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When adding an optional dependency (require(...) in a try/catch block), you need to put the try/catch exactly at the same location as the require(..).
The try/catch cannot be somewhere else where the require(..) is called through a function.
This breaks:
constgetModule=()=>require('bla')// <-- error thrown here alreadyletmodule=nulltry{module=getModule()}catch(e){console.log('module not available.')// <-- error is NOT caught here}
This works:
constgetModule=()=>{try{returnrequire('bla')}catch(e){console.log('module not available.')// <-- error caught safelyreturnnull}}letmodule=getModule()
The error thrown in example 1:
BUNDLE ./index.js
error: Error: Unable to resolve module ihgbewui from /Users/mrousavy/Projects/react-native-vision-camera/package/src/dependencies/WorkletsProxy.ts: ihgbewui could not be found within the project or in these directories:
../node_modules
../../node_modules
13 |
14 | const testProxy = createModuleProxy('ughire', () => {
> 15 | return require('ihgbewui')
| ^
16 | })
17 |
at ModuleResolver.resolveDependency (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:139:15)
at DependencyGraph.resolveDependency (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/node-haste/DependencyGraph.js:277:43)
at Object.resolve (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/lib/transformHelpers.js:169:21)
at Graph._resolveDependencies (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/Graph.js:473:35)
at Graph._processModule (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/Graph.js:261:38)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Graph._traverseDependenciesForSingleFile (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/Graph.js:249:5)
at async Graph.traverseDependencies (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/Graph.js:157:9)
at async DeltaCalculator._getChangedDependencies (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/DeltaCalculator.js:281:42)
at async DeltaCalculator.getDelta (/Users/mrousavy/Projects/react-native-vision-camera/package/example/node_modules/metro/src/DeltaBundler/DeltaCalculator.js:112:16)
What is the expected behavior?
I expect it to propagate the error upwards so I can catch it later.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
const{ getDefaultConfig, mergeConfig }=require('@react-native/metro-config')constpath=require('path')constescape=require('escape-string-regexp')constexclusionList=require('metro-config/src/defaults/exclusionList')constpak=require('../package.json')constroot=path.resolve(__dirname,'..')constmodules=Object.keys({ ...pak.peerDependencies})/** * Metro configuration * https://facebook.github.io/metro/docs/configuration * * @type {import('metro-config').MetroConfig} */constconfig={watchFolders: [root],// We need to make sure that only one version is loaded for peerDependencies// So we block them at the root, and alias them to the versions in example's node_modulesresolver: {blacklistRE: exclusionList(modules.map((m)=>newRegExp(`^${escape(path.join(root,'node_modules',m))}\\/.*$`))),extraNodeModules: modules.reduce((acc,name)=>{acc[name]=path.join(__dirname,'node_modules',name)returnacc},{}),},transformer: {getTransformOptions: async()=>({transform: {experimentalImportSupport: false,inlineRequires: true,},}),},}module.exports=mergeConfig(getDefaultConfig(__dirname),config)
The text was updated successfully, but these errors were encountered:
Yes, this is a limitation of "optional dependencies" at the moment.
The default expectation is that dependencies are non-optional and should throw at build time when missing, which is what you see in the first example. In general that's typically more likely to be a mistyped import or unintentionally missing npm dependency and failing fast is preferred.
Optional dependencies are a special case - when we can determine by single module static analysis (currently, this needs to be a try/catch directly around the require - which could be more sophisticated but with static analysis could never cover everything) that the missing dependency is handled at runtime, we infer that's your intention and effectively build the bundle with a hole in it.
One option is to try/catch locally (to let Metro know you're handing the missing dependency) and rethrow (to handle it elsewhere at runtime). Would that work for you?
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When adding an optional dependency (
require(...)
in a try/catch block), you need to put the try/catch exactly at the same location as therequire(..)
.The try/catch cannot be somewhere else where the
require(..)
is called through a function.This breaks:
This works:
The error thrown in example 1:
What is the expected behavior?
I expect it to propagate the error upwards so I can catch it later.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
The text was updated successfully, but these errors were encountered: