8

While trying to follow the Add Firebase to your JavaScript project on an empty git repository that will be used for a TypeScript project, I got the following error when I ran firebase deploy:

> functions@ build /Users/mosofsky/Documents/Developer/abcplan/functions
> tsc

src/index.ts:1:1 - error TS6133: 'functions' is declared but its value is never read.

1 import * as functions from 'firebase-functions';   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! functions@ build: `tsc` npm ERR! Exit status 2 npm ERR!  npm ERR! Failed at the functions@ build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR!     /Users/mosofsky/.npm/_logs/2019-09-06T03_00_54_557Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

Since I'm following Google's getting-started guide, I expected everything to work.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • The docs you linked to are for web project running in a web browser. Cloud Functions run on the backend and do not use the web client SDKs. Backend projects running Cloud Functions use the Firebase Admin SDK to access Firebase and Google Cloud products. – Doug Stevenson Sep 06 '19 at 03:45
  • @DougStevenson the documented steps led include `firebase init` which asked me which Firebase services I wanted (and which language). I chose all of the services listed and TypeScript because the instructions didn't says what to choose. One of them must have been Cloud Functions. Hence it generated `functions/src/index.ts` with the error. – Michael Osofsky Sep 06 '19 at 17:45
  • The Firebase CLI doesn't generate anything with respect the web client code (which is what you linked to). The CLI just deals with backend code. – Doug Stevenson Sep 06 '19 at 18:03
  • `firebase init` did generate `functions/src/index.ts` @DougStevenson – Michael Osofsky Sep 06 '19 at 18:23
  • Right, and that's backend code. It's not going to run in a web client. I'm assuming you wanted to run web client code because your question links to the web setup instructions. Perhaps you have the wrong link? – Doug Stevenson Sep 06 '19 at 20:34
  • You're correct, I wanted to make a web client. I just wanted to get up and running and I thought the link would have enough instruction to get up and running without hiccups. But there wasn't enough detail in the instructions to advise me not to choose Cloud Functions. I know that Cloud Functions run on the backend. But when I saw Cloud Functions as an option in the `firebase init`, I chose it because I assumed it would configure my web client to call the Cloud Function. Instead, according to your comments, it would init a backend project. So I shouldn't have chosen Cloud Functions. – Michael Osofsky Sep 06 '19 at 22:26
  • For a web project, you don't need the Firebase CLI at all. If you follow the instructions you linked, it should all happen within your HTML and JavaScript. The only way that the CLI would help you is if you need to serve your assets with Firebase Hosting, or if you want to deploy security rules outside the console. – Doug Stevenson Sep 07 '19 at 02:55
  • @DougStevenson are you responsible for communicating usability feedback to Google regarding their products, services, and documentation? Are you responsible for defending Google's reputation? I'm trying to understand what your role is. – Michael Osofsky Sep 07 '19 at 21:32
  • On Stack Overflow, I'm here to answer questions. It seemed to me there was some confusion about the purpose of the CLI, which I'm hoping to clear up. If there is still some confusion that can't be resolved, especially with the documentation, there is a "send feedback" link at the top of every page of Firebase docs that I would encourage you to fill out with your specific observations. As of now, I haven't heard of anyone getting derailed into the CLI as part of a path to web integration, so things are just very curious to me. I have nothing to defend, I'm just trying to help. – Doug Stevenson Sep 07 '19 at 21:44
  • I should clarify - I've never heard of anyone going down the path of configuring Cloud Functions from the CLI during web setup. If your intent is to deploy your web site to Firebase Hosting (which I can't really tell if that's your ultimate goal here), then you just use the CLI to configure Hosting. – Doug Stevenson Sep 07 '19 at 21:53
  • Thanks for clarifying @DougStevenson. My objective was simply to learn about Google Firebase for web apps because I may want to build one someday. I assumed by following the documentation I wouldn't run into problems. I think the documentation needs a bit more detail about how to answer the `firebase init` prompts but is otherwise pretty good. I just submitted feedback through the "send feedback" link, thanks for that suggestion. – Michael Osofsky Sep 07 '19 at 22:33

7 Answers7

15

The root cause is that a) a variable is declared but not used, and b) the TypeScript compiler is configured to flag unused variables as an error.

That configuration is overkill in a basic setup, in my opinion. Perhaps future edits to functions setup will fix this.

As others have pointed out, the ideal choice is simply to use the variable, i.e. make it "not unused".

Sometimes parameter to functions are needed or at least useful to declare but not use. In that case you can prefix with _, and then tell TypeScript that unused variables with a leading underscore can be ignored.

As a last resort, you could tell TypeScript to ignore all unused variables. Change this line in tsconfig.json so an unused variable is no longer an "error" - not recommended:

$ cat functions/tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,  // <-- change to false. Not recommended!
    ...
Andrew E
  • 7,697
  • 3
  • 42
  • 38
7

I figured out that I could just comment out the offending line until I write some code.

The file location is functions/src/index.ts from the repository's root directory (not src/index.ts).

The first line said

import * as functions from 'firebase-functions';

So I prefixed it // like this:

// import * as functions from 'firebase-functions';

Then I reran firebase deploy and I got a little farther (I got another error, unrelated but was solved by Error: HTTP error: 400, Project 'my_project' is not a Firestore enabled project).

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • After this step what works for me was on cd functions running "npm run build" and then firebase emulators:start – Andres R Jul 03 '22 at 06:26
6

I ran into the same issue. My problem was that I didn't save the changes in the file (uncommenting the helloWorld test code) before deploying. After saving, it deployed fine.

2

Either you can comment and deploy:

// import * as functions from 'firebase-functions'; 

Or you can uncomment following code and deploy:

 export const helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
 });
Pragati Dugar
  • 262
  • 3
  • 7
0

The error is complaining that you declared a variable but did not use it. Had you proceeded further in the tutorial to add in this line:

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

Then the error would have gone away. Also, that error is generated by the TypeScript compiler and the instructions are for JavaScript. It would be impossible to get such an error if you were using JavaScript.

Pace
  • 41,875
  • 13
  • 113
  • 156
  • But that's in step 5.2 of the tutorial. Earlier, in step 4.4 it calls for running `firebase deploy` which errors out because of the choices I made in `firebase init`. The tutorial didn't specify which options to choose in `firebase init`, so I choose TypeScript and all of the Firebase services. – Michael Osofsky Sep 06 '19 at 17:49
0

Just save your index.ts file and run firebase deploy

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 25 '23 at 13:33
-2

Run the following command and it will solve your problem.

npm install -g firebase-tools