eslint-import-resolver-babel-module icon indicating copy to clipboard operation
eslint-import-resolver-babel-module copied to clipboard

Still getting import/no-extraneous-dependencies error

Open itrelease opened this issue 7 years ago • 15 comments

In my .babelrc:

{
  "presets": ["env", "react"],
  "plugins": [
    "react-hot-loader/babel",
    "transform-class-properties",
    "transform-object-rest-spread",
    [
      "module-resolver",
      {
        "alias": {
          "@root": "./src/scripts/",
          "@components": "./src/scripts/components",
          "@reducers": "./src/scripts/reducers",
          "@utils": "./src/scripts/utils"
        }
      }
    ]
  ],
  "retainLines": true
}

in .eslintrc:

...
"settings": {
    "import/resolver": {
      "babel-module": {}
    }
  },
...

but eslint getting error ([eslint] '@components/Block' should be listed in the project's dependencies. Run 'npm i -S @components/Block' to add it (import/no-extraneous-dependencies)) on line:

import { DraggableBlock } from '@components/Block';

itrelease avatar Nov 25 '17 13:11 itrelease

I had the same issue but downgrading to these versions did the job for me:

"babel-plugin-module-resolver": "^2.5.0",
"eslint-import-resolver-babel-module": "^3.0.0",

QuentinBrosse avatar Nov 28 '17 16:11 QuentinBrosse

Below worked fine for me.

"settings": {
    "import/resolver": {
      "babel-module": {
        "root": ["./src"],
        "alias": { "config": "./config" }
      },
    }
  },

Found example in code: https://github.com/tleunen/eslint-import-resolver-babel-module/blob/master/src/index.js#L87

chuck0523 avatar Dec 08 '17 07:12 chuck0523

@tleunen are you able to address this issue ? I am also having the same problem and the above workarounds do not solve the problem

babel.rc

{
  "presets": ["react-native"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "@components": "./app/components",
          "@routes": "./app/routes",
          "@util": "./app/util",
        }
      }
    ]
  ]
}

eslint.js

settings: {
    'import/resolver': {
      node: {
        extensions: ['.js', '.android.js', '.ios.js'],
      },
      'babel-module': {},
    },
    node: true,
  },

ndillon1 avatar Jan 08 '18 13:01 ndillon1

@tleunen: can you shed some light on this? I thought the plugin wasn't loading the options but it is, not sure what's wrong but I'm getting the same error and everything seems to be configured right:

// .babelrc
{
  "presets": ["react-native"],
  "plugins": [
    [
      "module-resolver",
      {
        "cwd": "babelrc",
        "alias": {
          "@app": "./app",
          "@common": "./app/common",
          "@config": "./app/config",
        }
      }
    ]
  ]
}
// .eslintrc
{
  "parser": "babel-eslint",
  "plugins": [
    "react-native"
  ],
  "env": {
    "jest": true
  },
  "extends": [
    "airbnb",
    "plugin:react-native/all"
  ],
  "settings": {
    "import/resolver": {
      "react-native": {},
      "babel-module": {}
    }
  },
  "rules": {
    "import/no-extraneous-dependencies": ["error", {"devDependencies": ["__tests__/**/*.js", "__mocks__/**/*.js" , "storybook/**/*.js"]}],
  }
}
// package.json
"babel-plugin-module-resolver": "^3.0.0",
"eslint-import-resolver-babel-module": "^4.0.0",

lucasbento avatar Jan 09 '18 11:01 lucasbento

@lucasbento it seems the problem with @ symbol, so as a workaround you can prefix your stuff with src- or something else like:

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

itrelease avatar Jan 09 '18 12:01 itrelease

@itrelease: thanks, that's exactly it!

The problem is that I still want to use @, src- doesn't look very intuitive for me, do you know if there's an specific reason for it to not work?

Edit: I removed any prefix and kept only app/common/config, it works fine that way, good enough, thanks, @itrelease!

lucasbento avatar Jan 09 '18 12:01 lucasbento

I had the same problem with @ prefix. Sad it doesn't work.

Tomekmularczyk avatar May 11 '18 08:05 Tomekmularczyk

I'm facing same issue and I want to use "@"

tayfunyasar avatar May 11 '18 12:05 tayfunyasar

can confirm also issues with "@" sign

Edit: Sorry for lack of information.

Iam on: babel-plugin-module-resolver: ^5.0.0-beta.0 babel version: ^7.0.0-beta.51

Nopzen avatar Jun 14 '18 19:06 Nopzen

Any news on this ? Running into the same problem.

laugri avatar Jul 17 '18 15:07 laugri

How is this still not fixed :( I really want to use "@" to specify that the import is an alias and I'd rather not turn the rule off as it's a valid rule for every other use case.

ItsNoHax avatar Aug 23 '18 13:08 ItsNoHax

@ItsNoHax - Well. I believe we are all developers here. So if something bothers you because it's buggy, other devs will thank you if you actually try to fix it.

Having said that, I'll do my best to take a look at it by end of next week.

tleunen avatar Aug 23 '18 13:08 tleunen

Hey @tleunen, sorry if I came off rude.

Thanks for taking your time to look into it and if you need me to help you verify your solution is correct, just let me know.

ItsNoHax avatar Aug 27 '18 10:08 ItsNoHax

As workaround you may add "import/core-modules": "@your-package-name" to .eslintrc settings section

artemjackson avatar Sep 03 '18 09:09 artemjackson

I bumped with this issue today and I analysed the code and the problem is at the eslint-plugin-import, more specifically, here.

Our aliases matches with this regex, and this causes the plugin to recognise the import as an external scoped dependency (because it is the same format), so it tries to find it in node_modules and because it's not there it throws the error.

In the open issue the author implicitly says (as far as my interpretation goes) that we shouldn't name our aliases as the same as valid npm packages (or namespaces).

I don't know exactly to fix this without messing with the actual scoped external dependencies, because I didn't look to the rest of the code to see how it works as a whole, but I hope that this info someway helps.

ashton avatar Jan 30 '19 01:01 ashton