babel-plugin-module-resolver: it's can't use in react-native.
when i use it in my react-native project. and set .babelrc by this:
{ "presets": ["react-native"], "plugins": [ [ "module-resolver", { "root": ["./src"], "extensions": [".js", ".ios.js", ".android.js"], "alias": { "app": "./src", "assets": "./src/assets", "components": "./src/components", "containers": "./src/containers", "models": "./src/models", "routes": "./src/routes", "services": "./src/services", "utils": "./src/utils", "underscore": "lodash" }, } ] ] }
my project struct:

i changed the file of src/index.js and edit ‘import Toast from ‘./utils/toast’;’ to ‘import Toast from ‘utils/toast’;’
but the node console raise some error like this:

About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 4
- Comments: 55 (5 by maintainers)
@meyer-net have you try
yarn start -- --reset-cache?I found a simple solution for react native users and it’s NOT dependent on
babel-plugin-module-resolverjust create anotherpackage.jsonfile under the./srcfolderpackage.json
now the below code will work
import Button from 'src/components/Button';It’s not the best solution and I am not happy with it, but it works.
Faced with same problem. Like @meyer-net i’m using yarn(1.3.2) and have following setup:
package.json
.babelrc
project layout: .project_root/index.js
import App from '@src/App';.project_root/src/App.jsimport MyButton from "@util/base/button/Button";.project_root/util/base/button/Button.js .project_root/util/base/button/Button.native.jsAlso i’m using connected via usb physical device. When i run
react-native run-androiderror is thrown, BUT as @maxhungry said, if try to run withyarn start --reset-cachemodules begin to resolve. It even work if i choose to “Enable HOT Reloading” in dev menu. Ok, then i stop packager, launched byyarn start --reset-cacheand tryreact-native run-androidagain. It works for the first application start, and the same exception is thrown after first HOT reload 😭 As a temporary workaround i usereact-native run-android --no-packagerto compile project and install it on my device, thenyarn start --reset-cache. Well, it seems works so far, it’s not the best solution i guess and maybe something may go wrong further, but for simple project skeleton it works.@Gr1ver see this to make it work with React Native.
In short: none of the maintainers here use React Native. Of course we want to see this fixed, but we don’t have the time to go out of our way and set up a React Native project to investigate this issue.
But this very project (babel-plugin-module-resolver), being open source, welcomes contributions from any person who is willing to help. It’s just that so far nobody came to us with an actual fix for this problem.
Personally, since this problem has been such a long-standing one, it’s interesting that no one from the React Native community has stepped up yet to thoroughly investigate it. But when that person comes, all the glory to them - maybe it could be even you? You’d not only be helping yourself, but the whole community.
Hello all, I’m having the same issue. My
.babelrcis copied right from the docs.When I try to import, say, a component like so:
import Button from 'components/Button'…it throws out an error:
Module does not exist in the module map, when I try to run for iOS.I’ve searched for some possible solutions from other issues like resetting the packager cache but to no avail. Any ideas how to resolve this?
i have an example for guys. please read readme guide. https://github.com/hungdev/react-native-alias-example
Hi @ALL, here’s my way to solve this. This may be a little help.
CRNA (create-react-native-app) module resolve & eslint rules check
@meyer-net the reason that it can not resolve the module is the order of plugins in babelrc. If you are using decorator plugin, you have to add it after
module-resolver. This is importantreact-native run-ios -- --reset-cacheis not working, but starting the packager separatelyyarn start -- --reset-cacheis. Would be good to pass--reset-cachefrom react-native-cli to the packageri have solved this problem in my project.The reason for this problem is the wrong cwd when translate the relative dir. so there are two ways to solve it. 1、u can use npm run start – --reset-cache,but u must keep the terminal to continue the task. in other word, reset-cache do not work if u open a new terminal. this way is not recommended because it’s not persistent. 2、add a new config “cwd”:“babelrc” in .babelrc config like below(recommended):
Hey guys, This is my solution!
.babelrc.babelrc.jsI have it working just fine on React Native 0.56.1.
package.json.babelrcindex.android.jsPLEASE IGNORE THIS COMMENT from ME (@alinz)
Note from the maintainers of this project: this comment was not bringing us closer to solving the actual problem at hand in any way. Please refrain from adding similar comments - they will be removed in the future.
babel-plugin-module-alias@1.6.0decoratorif you are using itchange
@appto anything that you want to be used and load.@rendomnet I already listed the relevant versions used, the rest of the
devDependenciesshouldn’t make any difference.Just to double check, I ran
npm ls @babel/core babel-plugin-module-resolver react-native metro-react-native-babel-presetto get:And
react-native -vgives me:I got it working again. I’m on macOS (10.14), not using Expo, and my relevant module versions are:
react-native-cli@2.0.1react-native@0.57.7babel-plugin-module-resolver@3.1.1@babel/code@7.1.6My
.babelrc:I ran the following line first to be safe although not sure it’s necessary:
watchman watch-del-all && rm -rf /tmp/metro-bundler-cache-* && rm -rf /tmp/haste-map-react-native-packager-*Then I start the project using
react-native run-ioswhich results in an error without modules being resolved correctly. I cancel and close the metro bundler instance which opens in its own terminal and then runnpm run start -- --reset-cachefrom the project root. Then everything resolves correctly and subsequentreact-native run-ioscalls work as expected.My guess is that it’s really just the
--reset-cachefor the metro bundler that is required.I have tried several solutions here but it seems not work… I have a package that I want to use the module resolver for and I have configure babel/eslint/glow but it doesnt seems to work when i am importing form the package… Any help would be great at this point
To anyone having the same problem, please check whether transform-decorators-legacy is before or after module-resolver as @alinz said. Placing it after module-resolver solves the problem.
@skiral the problem with that is you need to run yarn install before every other command and also your code is copied to node_modules every time. You may do that to all folders and add file path packages. Yet you still need to yarn install every time and copy.
We are facing the same issue here, did anyone find a solution? Shall we use another module?
@hendrul thanks for letting me know, I didn’t check my facts.