4

I'm trying to use @redux/toolkit in my react-native app. However, when I run the iOS simulator I get the following error.

Metro has encountered an error: While trying to resolve module immer from file /path-node-modules/node_modules/@reduxjs/toolkit/dist/redux-toolkit.cjs.production.min.js, the package /path-node-modules//node_modules/immer/package.json was successfully found. However, this package itself specifies a main module field that could not be resolved (/path-node-modules/node_modules/immer/dist/immer.esm.mjs. Indeed, none of these files exist:

  • /path-node-modules/node_modules/immer/dist/immer.esm.mjs(.native|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json)
  • /path-node-modules/node_modules/immer/dist/immer.esm.mjs/index(.native|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json)

I found a similar issue on immer's github repo that was resolved in 2019, but I'm still seeing this today (which is weird because I have all the latest packages).

I edited the node-modules path, because it starts from my root directory.

Any ideas on how to resolve this?

Ashish Dommety
  • 152
  • 2
  • 13
  • I am facing the exact same issue. I too have modified my project structure. I guess it is because of modifying the structure because I tried it with a fresh project. There it runs without any issue. – Joshua May 12 '22 at 18:03

3 Answers3

2

Seems like adding mjs to the metro config did the trick.

const { getDefaultConfig } = require("expo/metro-config");

const defaultConfig = getDefaultConfig(__dirname);

defaultConfig.resolver.sourceExts = ['jsx', 'js', 'ts', 'tsx', 'mjs'];

module.exports = defaultConfig;
Ashish Dommety
  • 152
  • 2
  • 13
0

I just installed version of 9.0.12 for Immer and it fixed! I guess there is issue in version of 9.0.13.

0

if you use yarn, put this in your package.json:

  "resolutions": {
    "@reduxjs/toolkit/**/immer": "9.0.12"
  },

and do yarn install --force.