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

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.



Solution 1:[1]

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!
    ...

Solution 2:[2]

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).

Solution 3:[3]

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.

Solution 4:[4]

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!");
 });

Solution 5:[5]

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.

Solution 6:[6]

Run the following command and it will solve your problem.

npm install -g firebase-tools

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Michael Osofsky
Solution 3 Casey F. W. Hood
Solution 4 Pragati Dugar
Solution 5 Pace
Solution 6 Oshadha Punchihewa