Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Keep getting "ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed"

I download the lastest version of google cloud CLI, to use it with my firebase cloud function project (node, typescript), in order to deploy it with its service account JSON using this comand:

    gcloud functions deploy rgDev --runtime nodejs22 --trigger-http --service-account firebase-adminsdk-fbsvc@my-project.iam.gserviceaccount.com

 
but for some reason I always got this error:
 
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed with status: FAILURE and message: lib/index.js does not exist.
 
I checked the folder and it exists, the "main" property in package.json is correct, the tsconfig "outdir" too. For a little while I solved this changing the folder name from lib to build, but after a few deploy, it gave me the same error.
 
What can I do??
 
0 1 228
1 REPLY 1

Hi @TheNemus,

Welcome to Google Cloud Community!

It looks like there was an issue in getting your project transpiled from Typescript to Javascript.

As a starting point, make sure that your functions project is in accordance with the preferred directory structure as illustrated here. The index.ts file (the main source code) should be under src/ while the index.js file (the transpiled Javascript code) should be under lib/. Both src and lib are two folders under the same functions folder as their parent directory.

Another reason here is that maybe not all the typescript dependencies have been properly set up. You can check out these possible workarounds:

  • Reinstall typescript: See this Stack Overflow post and try reinstalling as suggested by Sami. Per Doug’s suggestion, it’s best to install it only in your project and not globally.
    sudo npm install -g typescript
    
  • Regenerate the existence of your ‘lib’ and ‘lib/index.js’: Run the command below which will help generate the ‘lib’ folder as well as the ‘lib/index.js’ file. Otherwise, you can check this other Stack Overflow discussion that will help regenerate the needed dependencies for your Typescript function.
    npm run-script build
    

Since you’re using GCloud CLI, try using Firebase CLI to deploy your function. Afterwards, follow this helpful guide (specifically from Firebase) to properly deploy your functions using Typescript. There are two sets of instructions that would help you set this up properly: (1) if you're using an existing Typescript project, or (2) if you were migrating an existing Javascript project to a Typescript project.

I’d also suggest saving a backup copy of your functions project before testing out these tips, or you can try creating a minimal, complete and verifiable example of a Typescript function code as part of your tests.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Top Solution Authors