23

I'm was trying to connect to my Firebase app by following the docs, but ESLint is complaining. I have already checked a related question, but the solutions proposed there doesn't seem to work for me. I have the following .eslint.js file:

module.exports = {
  extends: [
    'airbnb-typescript/base',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:typescript-sort-keys/recommended',
    'plugin:import/recommended',
    'prettier'
  ],
  parser       : '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      modules: true
    },
    ecmaVersion: 6,
    project    : './tsconfig.json',
    sourceType : 'module'
  },
  plugins: ['@typescript-eslint', 'typescript-sort-keys', 'sort-keys-fix'],
  rules  : { ... }
}
Anthony Luzquiños
  • 739
  • 11
  • 23
  • 1
    same issue for me after upgrading from firebase-admin 9 to version 10. I temporarly disabled the rule for this import statement with: ```// eslint-disable-next-line import/no-unresolved``` – cpc Nov 05 '21 at 16:03

4 Answers4

27

As of Firebase Admin v10 it uses exports in package.json for defining entry points. eslint-plugin-import does not support exports. Until it does you'll have to disable import/no-unresolved entirely or for each violation.

// eslint-disable-next-line import/no-unresolved
abraham
  • 46,583
  • 10
  • 100
  • 152
3

As mentioned in this github issue:#1359 It looks like import/no-unresolved is part of a custom ESLint plugin. I believe this is a bug/limitation in eslint-plugin-import (see issue #1868 ). The code is indeed valid, and compiled by tsc. VSCode can also successfully resolve the module entry points.

So I now just suppress the warning for firebase-admin using the following eslint rule:

'import/no-unresolved': [
      'error',
      {
        ignore: ['^firebase-admin/.+'],
      },
    ],

This worked for me as the solution is not yet merged in Eslint.

0

Here is how I managed to fix this error, the problem is that typescript don't find it but the package is actually there, so just added path to it.

{
  "compilerOptions": {
  ...
  "paths": {
    "firebase-admin/*": [
      "node_modules/firebase-admin/lib/*"
    ]
  }
}

My eslint config, since I made a while ago, I wan't to tell that only the setting part is important for the correct linking, but I have put all options relative to typescript in case.

module.exports = {
  ...,
  // not sure it is relevant for all projects
  parserOptions: {
    sourceType: 'module',
    project: ['./tsconfig.json'],
    tsconfigRootDir: __dirname,
  },
  parser: '@typescript-eslint/parser',
  
  // Important part
  settings: {
    'import/resolver': {
      typescript: {
        alwaysTryTypes: true,
        project: [
          './tsconfig.json',   // path to tsconfig
        ],
      },
    },
  },
};
gxmad
  • 1,650
  • 4
  • 23
  • 29
  • Original question is about Eslint error, not TypeScript. In my case also, TypeScript can successfully find the package, but `eslint-plugin-import` cannot. – AKd Dec 07 '22 at 17:31
  • I know, this fix the eslint error, if your eslint is correctly linked to your tsconfig – gxmad Dec 08 '22 at 19:31
  • Oh, really? Worth trying then. Would be useful to mention this connection in the answer itself. – AKd Dec 09 '22 at 17:18
  • @AKd just updated to response with my eslint config, tell me if that works for you too – gxmad Dec 10 '22 at 13:59
0

Per this issue, "By default eslint-plugin-import does not support exports property in package.json.

Link to related issue: import-js/eslint-plugin-import#1810

Solution is to add plugin: https://github.com/import-js/eslint-import-resolver-typescript which introduces the support."

e.g.

add

"import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"],
    },
    "import/resolver": {
      "typescript": {
        "alwaysTryTypes": true,
        "project": "./tsconfig.json",
      },
    },

to your eslintrc.js, and npm i -D eslint-plugin-import eslint-import-resolver-typescript

RYFN
  • 2,939
  • 1
  • 29
  • 40