3

Cloud function on my computer works with Javascript, but when I try it using TypeScript it does not compile to Javascript. It does not event create lib/index.js

When I run firebase deploy, it shows the error that

Error: There was an error reading functions/package.json:

firebase deploy --debug shows the following log:

APPLEs-MacBook-Air:functions abbasi$ firebase deploy --debug
[2020-01-08T08:39:06.383Z] ----------------------------------------------------------------------
[2020-01-08T08:39:06.390Z] Command:       /usr/local/bin/node /usr/local/bin/firebase deploy --debug
[2020-01-08T08:39:06.390Z] CLI Version:   7.11.0
[2020-01-08T08:39:06.390Z] Platform:      darwin
[2020-01-08T08:39:06.391Z] Node Version:  v12.14.1
[2020-01-08T08:39:06.392Z] Time:          Wed Jan 08 2020 13:39:06 GMT+0500 (Pakistan Standard Time)
[2020-01-08T08:39:06.393Z] ----------------------------------------------------------------------
[2020-01-08T08:39:06.393Z] 
[2020-01-08T08:39:06.422Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2020-01-08T08:39:06.423Z] > authorizing via signed-in user
[2020-01-08T08:39:06.426Z] [iam] checking project safepay-test for permissions ["cloudfunctions.functions.create","cloudfunctions.functions.delete","cloudfunctions.functions.get","cloudfunctions.functions.list","cloudfunctions.functions.update","cloudfunctions.operations.get","firebase.projects.get"]
[2020-01-08T08:39:06.429Z] >>> HTTP REQUEST POST https://cloudresourcemanager.googleapis.com/v1/projects/safepay-test:testIamPermissions  
 permissions=[cloudfunctions.functions.create, cloudfunctions.functions.delete, cloudfunctions.functions.get, cloudfunctions.functions.list, cloudfunctions.functions.update, cloudfunctions.operations.get, firebase.projects.get]
[2020-01-08T08:39:08.205Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Wed, 08 Jan 2020 08:39:08 GMT, server=ESF, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, server-timing=gfet4t7; dur=1374, alt-svc=quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000, accept-ranges=none, transfer-encoding=chunked

=== Deploying to 'safepay-test'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
Running command: npm --prefix "$RESOURCE_DIR" run build
✔  functions: Finished running predeploy script.
[2020-01-08T08:39:13.911Z] > [functions] package.json contents: {
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^3.2.2",
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

Error: There was an error reading functions/package.json:

 functions/lib/index.js does not exist, can't deploy Cloud Functions

Having trouble? Try firebase [command] --help

I have just tried it on another colleague's Macbook, it works on his system perfectly, but not on my system.

Kindly help me with this.

4
  • What error do you see when you use tsc by hand? Put npx tsc index.js. Commented Jan 11, 2020 at 12:01
  • what should I type in terminal?
    – Sami
    Commented Jan 11, 2020 at 13:05
  • npx tsc lib/index.ts Commented Jan 11, 2020 at 13:23
  • It has been resolved.
    – Sami
    Commented Jan 11, 2020 at 13:26

4 Answers 4

5

I was having the same problem: Typescript just wouldn't compile. Logging a lot of errors, mostly like this one here: "error TS2300: Duplicate identifier 'AbortController'"

The solution:

Just added the following line inside the "compilerOptions" property in the tsconfig.json file:

"typeRoots": ["node_modules/@types"] 

Like that:

/functions/tsconfig.json


{ 
  {
    "compilerOptions": {
      "module": "commonjs",
      "noImplicitReturns": true,
      "noUnusedLocals": true,
      "outDir": "lib", 
      "sourceMap": true,
      "strict": true,
      "target": "es2017",
      "typeRoots": ["node_modules/@types"]  // <-- here
    },  
    "compileOnSave": true,
    "include": ["src"]
  }
}


After that it compiled just fine!

1
  • 1
    Your solution worked for me. I think it happens when the parent folder is also a TypeScript project, with it's own tsconfig.json etc.
    – ncuillery
    Commented May 10, 2022 at 18:55
1

If you clone a repo that contains a node project, including a Cloud Functions project, the first thing you should do is change to the directory where package.json is defined, and run npm install. This will rebuild the contents of node_modules which was not checked into source control. If you don't run npm install, then the project will not actually know anything about any of the modules defined in package.json.

0

I came to know that Typescript was not installed.

Resolved with

sudo npm install -g typescript

Now it is working ifne

1
  • 3
    You shouldn't have to install typescript globally like this. It just needs to be installed in your project, defined in package.json, which it was. Maybe you never ran npm install in your project to actually install it. Commented Jan 11, 2020 at 17:44
0

This solved it for me

after trying many solutions to get the build working, it worked when I uncommented the code in index.ts

import * as functions from "firebase-functions";

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
//   functions.logger.info("Hello logs!", {structuredData: true});
//   response.send("Hello from Firebase!");
// });

It was caused because the imported functions variable was declared but not used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.