5
votes

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.

2
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 hereAkshay Jain
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.Yasmine
Can you please share what is it that is not working? What do you need this package for?Akshay Jain
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
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.mesqueeb

2 Answers

6
votes

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.

2
votes

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');