mysticatea / eslint-plugin-node

Additional ESLint's rules for Node.js
MIT License
961 stars 171 forks source link

Support eslint-import-resolver-alias Config #249

Open jhoffmcd opened 3 years ago

jhoffmcd commented 3 years ago

Request to support module aliases as provided by the config in eslint-import-resolver-alias. It seems like the rule node/no-missing-import is not aware of aliases configured by the eslint-import-resolver-alias settings.

Example configuration:

{
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "sourceType": "module"
  },
  "extends": [
    "eslint:recommended",
    "plugin:node/recommended",
    "plugin:jest/recommended",
    "prettier"
  ],
  "plugins": ["prettier"],
  "settings": {
    "import/resolver": {
      "alias": {
        "map": [["~", "./src"]],
        "extensions": [".js", ".json"]
      }
    }
  },
  "rules": {
    "prettier/prettier": "error",
    "node/no-unsupported-features/es-syntax": [
      "error",
      {
        "version": ">=12.10.0",
        "ignores": ["modules"]
      }
    ]
  }
}

Still produces an error with this statement:

import { logger } from "~/utils/logging";

// error  "~/utils/logging" is not found  node/no-missing-import
jhoffmcd commented 3 years ago

Also filed an issue over at the alias plugin because I am not sure where the change would be needed:

https://github.com/johvin/eslint-import-resolver-alias/issues/20

Airkro commented 3 years ago

Turn off rules duplicate with eslint-plugin-import, use eslint-plugin-import directly might be a better solution.

lamuertepeluda commented 3 years ago

@Airkro what do you mean exactly? Like

"node/no-missing-require": "off", ?

Airkro commented 3 years ago

@lamuertepeluda

Yep. install eslint-plugin-import

+ 'import/no-extraneous-dependencies': 'error',
- 'node/no-extraneous-require': 'error',
- 'node/no-extraneous-import': 'error',
+ 'node/no-extraneous-require': 'off',
+ 'node/no-extraneous-import': 'off',

+ 'import/no-unresolved': 'error'
- 'node/no-missing-require': 'error',
- 'node/no-missing-import': 'error',
+ 'node/no-missing-require': 'off',
+ 'node/no-missing-import': 'off',

now you can use eslint-import-resolver-x

lamuertepeluda commented 3 years ago

@Airkro thank you!! You pointed me in the right direction! 😃 I have a node-express only project.

I did it like this in my root .eslintrc.json file and it's working

{
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:node/recommended",
  ],
  "plugins": ["import", "node"],
  "root": true,
  "env": {
    "es6": true,
    "browser": false,
    "node": true,
    "commonjs": true
  },
  "settings": {
    "import/resolver": {
      "alias": {
        "map": [
          ["@src", "./src"],
          ["@api", "./src/api"],
          ["@migrations", "./src/migrations"],
          ["@utils", "./src/utils"],
          // customize as you need
        ],
        "extensions": [".js", ".json", ".node"] // customize as you need
      }
    }
  },
  "rules": {
     // place other rules here
     // This one handles require() resolution respecting aliases IF you set commonjs: true
    "import/no-unresolved": ["error", { "commonjs": true }],
    "import/no-extraneous-dependencies": "error",
    // Turned off because conflicts with the ones above and does not support aliases
    "node/no-missing-require": "off",
    "node/no-extraneous-import":"off",
    // place other rules here
  }
}

and I am using module-alias and eslint-import-resolver-alias in addition to this.

quantuminformation commented 8 months ago

we get this:

Oops! Something went wrong! :(

ESLint: 8.57.0

EslintPluginImportResolveError: unable to load resolver "alias".
Occurred while linting /Users/nikos/WebstormProjects/agcra-app/app/components/AdminNavTabs.tsx:1
Rule: "import/namespace"
    at requireResolver (/Users/nikos/WebstormProjects/agcra-app/node_modules/.pnpm/eslint-module-utils@2.8.1_@typescript-eslint+parser@6.21.0_eslint-import-resolver-node@0.3.9__kttqjlkde72ifr4qchnoh6ko5e/node_modules/eslint-module-utils/resolve.js:199:17)
scagood commented 8 months ago

We have a supported version of this plugin over at https://github.com/eslint-community/eslint-plugin-n

We do support aliases from both typescript and the package.json.

If you need anymore settings we'd be happy to help!