Could not find a declaration file for module 'firebase-tools'
Asked Answered
R

4

13

I am writing my first cloud function for Firebase and it requires the firebase-tools module. I've installing it by adding it to my dependencies in the package.json file and running npm install.

Then I tried to import it using import * as tools from 'firebase-tools';, but I get this error:

Could not find a declaration file for module 'firebase-tools'. 'c:/Users/LENOVO/Nouveau dossier/functions/node_modules/firebase-tools/lib/index.js' implicitly has an 'any' type. Try npm install @types/firebase-tools if it exists or add a new declaration (.d.ts) file containing `declare module 'firebase-tools';

I also tried running npm install @types/firebase-tools, but apparently it does not exist and I'm not sure what I should put in the (.d.ts) file for this module.

So I'm asking if there's another solution and if I need to create a (.d.ts) file what should I put there beside declare module 'firebase-tools.

Ryannryazan answered 26/4, 2020 at 15:51 Comment(5)
Hey, firebase-tools is a command line interface tool and not be included in package.json. Just run "npm install -g firebase-tools" to install the tools. And now use "firebase init" to create a new firebase project. Complete documentation is present hereCaesarea
Hey Akshay , thanks for your answer. But I already did that and still it doesn't work. I included the dependency in my package.json file because it's what they did in this link: github.com/firebase/snippets-node/blob/master/firestore/…. And it is said that it can be used as a module over here too : npmjs.com/package/firebase-tools. I'm not sure what I'm missing, please correct me if I'm wrong.Ryannryazan
Can you please share what is it that is not working? What do you need this package for?Caesarea
When I try to import the package I get an error saying that there is no declaration file for the package. And I'm trying to write a cloud function for Firebase that does the deletion of a collection and its subcollections for Firestore and from what I found this requires this package. When I open the lib folder of the package that I've installed there's a folder called 'firestore' and a JavaScript file called 'delete' , so i guess that what I need in my case.The other packages has there own declaration files with the extension (.d.ts) but there's none for this one.Ryannryazan
Hey everyone, the docs here clearly say we need to use firebase-tools inside a cloud function. But since my cloud functions are TS, I'm also confused on how I can execute this.Winne
D
9

I have same problem, too. The problem is that firebase-tools modules don't have (.d.ts)file. I found that we have 3 solutions in this situation.

  • install @types/xxx ←you have done, but it doesn't exist.
  • self made (.d.ts)file ←I don't know very well.
  • use "require" instead of "import" ←I solved this way.The modules are imported as "any" type implicitly.

when ts-lint alert you "[tslint] require statement not part of an import statement (no-var-requires)", you can ignore it by comment "// tslint:disable-next-line:no-var-requires"

Thank you for reading.

Dichromatism answered 10/5, 2020 at 9:30 Comment(2)
do you know why it doen'st have a .d.ts file? Why don't they include typescript support by default for all their projects?Vanzandt
@Vanzandt based on the discussion on their GitHub issue it seems the amount of runtime code-gen makes a type definition file difficult to produce. github.com/firebase/firebase-tools/issues/2378Cribbs
R
2

The way I solved this problem was:

First of all, add "firebase-tools": "^9.10.0" to your package.json under /functions directory, like so:

"dependencies": {
...
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.13.1",
"firebase-tools": "^9.10.0"
},

Then, in your function code use require instead of import, like so:

const firebase_tools = require('firebase-tools');

Rabe answered 30/4, 2021 at 23:41 Comment(0)
V
1

try to add "noImplicitAny": false, to tsconfig.json

Viquelia answered 15/8, 2023 at 17:15 Comment(0)
A
0

This is currently open as issue #2378 (as of May, 2024). Until that is resolved and the project provides official types, you can manually create a module declaration with just enough type definitions to access a single command like this:

Create a new file types/FirebaseTools.d.ts. I determined the parameter names and types by looking at the output of firebase <command> -h.

declare module "firebase-tools" {

  // here we expose just enough to get the database:remove command to work in Typescript
  interface FirebaseClient {

    database: {
      remove: (path: string, options?: DatabaseRemoveOptions) => Promise<void>;
    };

  }
  const client: FirebaseClient;
  export default client;

  // global program options
  interface FirebaseToolsOptions {
    /** the Google account to use for authorization */
    account?: string;
    /** the Firebase project to use for this command */
    project?: string;
    /** output JSON instead of text, also triggers non-interactive mode */
    json?: boolean;
    /** error out of the command instead of waiting for prompts */
    "non-interactive"?: boolean;
    /** print verbose debug output and keep a debug log file */
    debug?: boolean;
  }

  // options specific to database:remove
  interface DatabaseRemoveOptions extends FirebaseToolsOptions {
    /** bypass confirmation prompt */
    force?: boolean;
    /** use the database <instance>.firebaseio.com (if omitted, use default instance) */
    instance?: string;
    /** suppress any Cloud functions triggered by this operation */
    "disable-triggers"?: boolean;
  }
}

Then add a mapping to paths inside tsconfig.json

{
  // ...
  "compilerOptions": {
    // ...
    "paths": {
      "firebase-tools": ["./types/FirebaseTools.d.ts"],
    },
  },
}

Now you can import the package as a module and access the particular command described in the type definition:

import FirebaseCLI from "firebase-tools";

// ...

await FirebaseCLI.database.remove("/foo/bar", {
  "non-interactive": true,
  force: true,
  "disable-triggers": true,
});
Archimedes answered 8/5 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.