Unable to resolve path to module 'firebase-admin/app' (ESLint)
Asked Answered
G

4

26

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  : { ... }
}
Gaut answered 27/10, 2021 at 23:41 Comment(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-unresolvedEsmond
C
31

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
Cultigen answered 14/11, 2021 at 1:32 Comment(0)
T
6

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.

Toluate answered 30/3, 2023 at 17:10 Comment(0)
N
4

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

Newcomer answered 15/12, 2022 at 7:46 Comment(0)
P
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
        ],
      },
    },
  },
};
Polestar answered 30/11, 2022 at 19:3 Comment(4)
Original question is about Eslint error, not TypeScript. In my case also, TypeScript can successfully find the package, but eslint-plugin-import cannot.Iguanodon
I know, this fix the eslint error, if your eslint is correctly linked to your tsconfigPolestar
Oh, really? Worth trying then. Would be useful to mention this connection in the answer itself.Iguanodon
@Iguanodon just updated to response with my eslint config, tell me if that works for you too 🤞Polestar

© 2022 - 2024 — McMap. All rights reserved.