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;
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", ], };