skip to Main Content

What is the command to enable ESLint for my cloud functions?

For a bit of background info, I ran firebase init and did the set up, but for some reason it was using ESLint instead of TSLint. I redid the firebase init and this time I answered "No" when it asked if I wanted to use ESLint. However, as the comments pointed out, ESLint is now favored and TSLint is deprecated. So now I need to re-enable ESLint for this project.

2

Answers


  1. This Video below is a great starter on how to set up your cloud functions including your mentioned eslint, typescript, very informative
    Explanation

    Login or Signup to reply.
  2. By taking a look at the repository for the firebase-tools utility, you can find the templates used in the firebase init command.

    For linting with eslint, you will need the following files:

    Once you have updated those files appropriately, run npm install in your functions directory so that the required libraries are downloaded.

    You will also need to add these lines to your firebase.json file in your project directory so that your functions are linted and transpiled before deployment.

    // PROJECT_DIR/firebase.json
    {
      "functions": {
        "source": "functions",
        "predeploy": [
          "npm --prefix "$RESOURCE_DIR" run lint",
          "npm --prefix "$RESOURCE_DIR" run build"
        ],
        /* ... */
      },
      /* ... */
    }
    

    .eslintrc.js, package.json, and tsconfig.dev.json, as they were at the time of writing, have been included below for convienience. Please consult their respective links if obtaining them in the future as they may have changed.

    // PROJECT_DIR/functions/.eslintrc.js
    module.exports = {
      root: true,
      env: {
        es6: true,
        node: true,
      },
      extends: [
        "eslint:recommended",
        "plugin:import/errors",
        "plugin:import/warnings",
        "plugin:import/typescript",
        "google",
        "plugin:@typescript-eslint/recommended",
      ],
      parser: "@typescript-eslint/parser",
      parserOptions: {
        project: ["tsconfig.json", "tsconfig.dev.json"],
        sourceType: "module",
      },
      ignorePatterns: [
        "/lib/**/*", // Ignore built files.
      ],
      plugins: [
        "@typescript-eslint",
        "import",
      ],
      rules: {
        "quotes": ["error", "double"],
        "import/no-unresolved": 0,
      },
    };
    
    // PROJECT_DIR/functions/package.json
    {
      "name": "functions",
      "scripts": {
        "lint": "eslint --ext .js,.ts .",
        "build": "tsc",
        "build:watch": "tsc --watch",
        "serve": "npm run build && firebase emulators:start --only functions",
        "shell": "npm run build && firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log"
      },
      "engines": {
        "node": "16"
      },
      "main": "lib/index.js",
      "dependencies": {
        "firebase-admin": "^10.0.2",
        "firebase-functions": "^3.18.0"
      },
      "devDependencies": {
        "@typescript-eslint/eslint-plugin": "^5.12.0",
        "@typescript-eslint/parser": "^5.12.0",
        "eslint": "^8.9.0",
        "eslint-config-google": "^0.14.0",
        "eslint-plugin-import": "^2.25.4",
        "firebase-functions-test": "^0.2.0",
        "typescript": "^4.5.4"
      },
      "private": true
    }
    
    // PROJECT_DIR/functions/tsconfig.dev.json
    {
      "include": [
        ".eslintrc.js"
      ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search