Michael Zur
Michael Zur

Reputation: 1145

Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions

Problem

Right after my TypeScript project initialization in VSCode using firebase tools for composing Firebase Cloud Functions following the official documentation the very first line of the index.ts file displays an error:

Parsing error: Cannot read file '\tsconfig.json' eslint [1,1] enter image description here

and the .eslintrc.js displays an error:

File is a CommonJS module; it may be converted to an ES6 module.ts(80001) enter image description here

Since all files are auto-generated these errors are a complete surprise and I want to get rid of them.

Versions

For the record, here are the versions installed:

npm      --version 8.1.3
tsc      --version 4.4.4
node     --version 17.0.1
firebase --version 9.22.0

Installation process

These are the commands I used in the powershell in VSCode with some info/warnings:

>npm install -g firebase-tools
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
...

>firebase init
...
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: undefined,
npm WARN EBADENGINE   required: { node: '14' },
npm WARN EBADENGINE   current: { node: 'v17.0.1', npm: '8.1.3' }
npm WARN EBADENGINE }
...

>npm install firebase-functions@latest firebase-admin@latest --save
...
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
...

>firebase deploy
...
C:\Users\SAMS\firebase_dogout\functions\src\index.ts
  1:13  warning  'functions' is defined but never used  @typescript-eslint/no-unused-vars
...
Error: functions predeploy error: Command terminated with non-zero exit code2

Files

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

.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,
  },
};

index.ts

import * as functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

What I tried

  1. Reinitialization
  2. Creating the .vscode folder with settings.json file with
 {
   "eslint.workingDirectories": [
       "src" // and "functions"
   ]
 }
  1. Updating your eslintrc.json file with the following line: "project":"PROJECT_NAME/tsconfig.json"
  2. Updating .eslintrc.js by setting tsconfigRootDir: __dirname in parserOptions
  3. Deleting the ESLint extension. The error was gone, but firebase deploy command didn't allow the code deployement.

So, the related thread didn't really help

Upvotes: 34

Views: 28284

Answers (4)

john
john

Reputation: 1998

i have the same error and added this line tsconfigRootDir: __dirname, inside parserOptions in the .eslintrc.js file as others suggested then the error went away.

Upvotes: 4

Denis Alarie 4RP
Denis Alarie 4RP

Reputation: 349

I read the error message and studied it to death. It was still popping up even after I had set up a basic project in firebase and tried to run it. Finally in desperation I removed COMMENTED out a line from my

eslintrc.js

project: ["tsconfig.json", "tsconfig.dev.json"], //REMOVE BY commenting out

and so my .eslintrc.js file was now reading as

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,
      },
    };


And just to make sure I was not out to lunch 
I cleaned up the index.ts file as per recommedations and then I ran it...


PS C:\ac\x> firebase deploy --only "functions:helloWorld"
=== Deploying to '***********'...
i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint --ext .js,.ts .
Running command: npm --prefix "$RESOURCE_DIR" run build
> build
> tsc
+  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged C:\ac\x\functions (87.87 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: creating Node.js 16 function helloWorld(us-central1)...
+  functions[helloWorld(us-central1)] Successful create operation.
Function URL (helloWorld(us-central1)): https://us-central1-xxxxxxx-xx.cloudfunctions.net/helloWorld
i  functions: cleaning up build files...
  • Deploy complete!

I wasted a lot of time on this and I hope this is the end of it....

Upvotes: 7

Denis Alarie 4RP
Denis Alarie 4RP

Reputation: 349

the following steps worked for me. I am not sure if they were all necessary but eventually I got to where I wanted to be. The steps previously mentioned here were not that helpful in my case probably because there can be many causes leading to the same error message. Finding a solution is sometimes a trial and error process until a solution is found.

In my case

  1. I updated my nodejs at nodejs.org/en/download.

    nodejs.org/en/download

then I reloaded and reinitialized my Firebase functions project. And then I updated the typescript parser.

npm i @typescript-eslint/parser

and at last it seems to work. I will keep my fingers crossed that this is indeed the end of this painful two day process.

Upvotes: 1

Michael Zur
Michael Zur

Reputation: 1145

Ok, I have solved the problem with a great help of this github thread False positive Error - TS6133 error (declared but its value is never read) report.

I have changed "noUnusedLocals" setting in the tsconfig.json file from the default true to false, so the file becomes:

"compilerOptions": {
    ...
    "noUnusedLocals": false,
    ...
  }

However, the strange thing is that after successfully deploying functions to Firebase Cloud Fuctions using this setting and afterwards reverting the changes, the redeployment doesn´t show any error and is successful as well.

Update

After retrying it a few time I must admit that the solution is different. Turns out that the related stackoverflow thread did help, in particular @cherryblossom solution. Setting tsconfigRootDir: __dirname in the .eslintrc.js file and restarting the VSCode solves the problem.

.eslintrc.js:

module.exports = {
  // ...
  parserOptions: {
    ...
    tsconfigRootDir: __dirname,
    ...
  },
  // ...
}

Upvotes: 65

Related Questions