How do I resolve new ESLint errors in my Cloud Functions project?
Asked Answered
L

5

24

Another day another question regarding Firebase:

I am relatively new to Firebase/node.js/npm ...

I'm currently trying to build a mobile app backend with Firebase for a university project. I use node.js and write, not test my functions locally, then I deploy them using the Firebase CLI and "firebase deploy". Everything worked out fine until I started working like one hour ago. I had a small error in my code which I was easily able to fix. In the same "deploy-cycle", Firebase CLI has shown me an available update for firebase-tools suggesting me to use

npm install -g firebase-tools

This is what I did and where my tragedy began. CLI then recommended to uninstall/reinstall

    npm uninstall -g @google-cloud/functions-emulator
    npm install -g @google-cloud/functions-emulator

So I did. Then I tried to deploy my functions (from local index.js) and received the following waring:

Error: Firebase config variables are not available. Please use the latest version of the Firebase CLI to deploy this function

I used npm install -g firebase-tools in order to update firebase CLI but nothing changed.

I thought it would be a good idea to just backup my old project and initialize a new one in a new folder and to connect it to my existing FB-Project and just copy-paste my the content of my old index.js to the one in the new project folder. Trying to deploy this new project, I received a ton of errors and warning concerning my code, like:

18:4   error    Expected catch() or return                  promise/catch-or-return

and many more, even though my coded functions worked fine before.

So I decided that it would probably be the best to try and fix the error with the old project.

Does anyone have a recommendation what to do in this case or where to find these mysterious firebase config variables? I wasn't able to find any solution to my problem online. I'd really appreciate any kind of helping support, since I have no idea what to do and I have no support from my university doing this...

Lantern answered 23/1, 2018 at 18:5 Comment(0)
I
49

The error message about config variables asked you to update your Firebase CLI, which you did. And that's fine. (You could have ignored the warning about @google-cloud/functions-emulator.)

When you created a new project, you probably chose to use ESLint during the project creation process. The prompt would have said:

Do you want to use ESLint to catch probable bugs and enforce style?

This is a very new feature in the Firebase CLI since version 3.17.0, and you probably weren't using ESLint previously.

Those new errors you're seeing are ESLint telling you there are potential problems with your code. The Firebase team strongly recommends you take the advice of those warnings and errors and resolve them, so that your code has fewer problems.

If you simply cannot use ESLint right now, you can re-create your project, but choose not to use ESLint when it prompts you. Or you can disable it in your current project by editing your firebase.json file and removing the predeploy script that runs the lint command.

Imperfect answered 23/1, 2018 at 18:12 Comment(3)
!! Thats it. For some reason my brain decided that letting ESLint catch probable bugs and enforce style would be a good idea, not knowing that this would result in the upcoming errors/warnings. I set up a new project and connected it with my existing fb-project and I'm finally able to deploy the functions from this new local project again. Modifying my functions with more robustness and using try/catch is sth I'll focus on in the close future, but for now it's important, that i can work and we can test our frontend. Thanks for the answer, it really helped a lot!Lantern
I tried removing the predeploy script and it worked fine. Just want to ask if I should resolve the eslint errors before publishing it? They are identation and newline errorsDishonorable
I just want it to auto-fix all style issuesKetti
S
2

In some cases you might want to throw in the towel all together but just want to ignore linting for the moment till you can address the issue it is having.

https://masteringjs.io/tutorials/eslint/ignore#:~:text=Disabling%20ESLint%20With%20a%20Comment&text=If%20you%20put%20%2F*%20eslint,the%20top%20of%20a%20file.

Using a .eslintignore can skip entire files that may not be ready to passing eslint but you still need to deploy for testing or what not.

Striation answered 13/5, 2021 at 16:35 Comment(0)
H
2

Just turn off pre-deploy commands in your firebase.json file: this line

  "predeploy": [
    "npm --prefix \"$RESOURCE_DIR\" run lint",
    "npm --prefix \"$RESOURCE_DIR\" run build"
  ]

remove the lint option: to have only build:

  "predeploy": [
    "npm --prefix \"$RESOURCE_DIR\" run build"
  ]

Note: all EsLint linting benefits will all be disabled and your project will not be linted for any errors

Heartstricken answered 27/9, 2023 at 10:12 Comment(0)
B
1

it looks like there are 2 things going on there:

1) You may not have the latest firebase-functions, and that's what's causing the error about the config variables. Try running the following in your functions folder prior to deploying again:

npm i --save [email protected]
npm i --save [email protected]

2) "Expected catch() or return" is a linter error meaning that you are not returning your promise chain. (This will lead to timeout errors in your deployed functions). So you should modify your code to look more like:

return asynchronousTask().then(function() {
 ....
   return someValue;
});
Bend answered 23/1, 2018 at 18:16 Comment(2)
If I try to update firebase-functions and firebase-admin in my project folder, the CLI always tries to read a package.json file located in my profile folder, but the only thing existing there is a package-lock.json. In my project folder however, a file named package.json file exists. Do I need to change the directory in the "update-command"? I'm running the command in terminal in my project folder.Lantern
Sorry, I cant edit the first comment anymore. I found the short term solution with Dougs answer but it would still be nice to get your thoughts on my first comment in order to understand more about the working practice of firebase/node.js. I am using asynchronous functions all over my functions in order to ensure no timeout errors are occurring. Thanks in advance Lauren! Would love to give you an upvote but my missing reputation points unfortunately don't allow me to...Lantern
C
0

you can type

/* eslint-disable */

on the top of the index.js file

Campobello answered 30/7, 2024 at 8:23 Comment(1)
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.Superorder

© 2022 - 2025 — McMap. All rights reserved.