Babel-plugin-module-resolver: Resolve local file instead of from node_modules

Created on 10 Jun 2018  路  3Comments  路  Source: tleunen/babel-plugin-module-resolver

"babel-plugin-module-resolver": "^3.1.1"

.babelrc

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    [
      "module-resolver",
      {
        "root": [
          "./src"
        ]
      }
    ],
    "relay"
  ]
}

Project file structure

  app
    components
       Button
       Modal
       ...

when I do import like this import {Modal} from 'react-native'; somehow I import Modal from my project instead of from 'react-native'. Why??
How to prevent babel-plugin-module-resolver to go in deep? I want only to resolve app and nothing else, so resolver should care only about import starting from app like import ... from 'app/...';

Most helpful comment

I ended up with removing key root and added alias like:

"alias": {
        "app": "./src/app",
         "config": "./src/config",
          ....
}

so now imports work as I expected:
import Modal from 'app/components/Modal';
or
import {Modal} from 'react-native'; - now plugin resolved files as it should.

Looks like root key is not mandatory and it creates issue with correct file resolving.

All 3 comments

Have you got any solution yet ! ?

I ended up with removing key root and added alias like:

"alias": {
        "app": "./src/app",
         "config": "./src/config",
          ....
}

so now imports work as I expected:
import Modal from 'app/components/Modal';
or
import {Modal} from 'react-native'; - now plugin resolved files as it should.

Looks like root key is not mandatory and it creates issue with correct file resolving.

@ViktorAksionov Out of curiosity, would you have a small repro project with the issue? This is the first time I hear that the root configuration is making a deep resolution...

Was this page helpful?
0 / 5 - 0 ratings