12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yasmine
  • 131
  • 1
  • 3
  • 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 [here](https://firebase.google.com/docs/cli) – Akshay Jain Apr 26 '20 at 16:34
  • 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: https://github.com/firebase/snippets-node/blob/master/firestore/solution-deletes/functions/package.json. And it is said that it can be used as a module over here too : https://www.npmjs.com/package/firebase-tools. I'm not sure what I'm missing, please correct me if I'm wrong. – Yasmine Apr 26 '20 at 17:27
  • Can you please share what is it that is not working? What do you need this package for? – Akshay Jain Apr 26 '20 at 17:33
  • 1
    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. – Yasmine Apr 26 '20 at 19:33
  • 2
    Hey everyone, the docs [here](https://firebase.google.com/docs/firestore/solutions/delete-collections#cloud_function) 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. – mesqueeb May 05 '20 at 22:04

3 Answers3

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.

  • 1
    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? – Ruben Jul 20 '21 at 17:10
  • @Ruben based on the discussion on their GitHub issue it seems the amount of runtime code-gen makes a type definition file difficult to produce. https://github.com/firebase/firebase-tools/issues/2378 – mbaker3 Apr 26 '23 at 02:33
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');

CloudWindMoonSun
  • 372
  • 1
  • 4
  • 13
1

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

Shimi Shimson
  • 990
  • 9
  • 10