If expo or react-native-cli is not able to find a file extension it will throw the error while trying to resolve module none of these files exist. To resolve this we need to let Expo and react-native-cli know about such extensions.
Solution with Code Example
If you are using Expo, then open or create metro.config.js
and update according to this –
const { getDefaultConfig } = require("@expo/metro-config"); const defaultConfig = getDefaultConfig(__dirname); defaultConfig.resolver.assetExts.push("cjs"); module.exports = defaultConfig;
![Adding extensions like cjs to metro.config.js file of Expo](https://i0.wp.com/authors.akashmittal.com/wp-content/uploads/2022/08/adding-extensions-in-expo-metro-config-file.png?resize=820%2C355&ssl=1)
You can add more extensions there.
For react-native-cli –
const { getDefaultConfig } = require("metro-config"); const { resolver: defaultResolver } = getDefaultConfig.getDefaultValues(); exports.resolver = { ...defaultResolver, sourceExts: [ ...defaultResolver.sourceExts, "cjs", ], };