Unable to deploy Cloud Functions - missing .json file

Encountering deployment issues due to importing a .json credential file for functions.

  1. During the initialization of Firebase functions, I opted for TypeScript.
    1. Subsequently, after configuring my functions in TypeScript, I attempted to import the .json file containing the project credentials necessary for firebase-admin usage.

/functions (Folder)

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es6"],
    "module": "commonjs",
    "noImplicitReturns": true,
    "outDir": "lib",
    "sourceMap": true,
    "target": "es6",
    "types" : [ "node" ],
    "esModuleInterop": true,
    "resolveJsonModule": true,
  },
  "compileOnSave": true,
  "include": [
    "src",
    "./typings.d.ts"
  ]
}

typings.d.ts:

declare module "*.json" {
    const value: any;
    export default value;
  }

package.json:

{
  "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"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.0.3"
  },
  "devDependencies": {
    "tslint": "~5.8.0",
    "typescript": "~2.8.3"
  },
  "private": true
}

functions/src/

-- serviceAccountKey.json

-- index.ts

index.ts:

import * as admin from 'firebase-admin';
import * as serviceAccount from './serviceAccountKey.json';

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "DATABASE-URL"
});

The deployment fails when attempting to import the file, whereas without the import everything functions normally.

Error:

i  deploying functions
Running command: npm --prefix "functions" run lint

> functions@ lint Z:\functions
> tslint --project tsconfig.json

Running command: npm --prefix "functions" run build

> functions@ build Z:\functions
> tsc

tsconfig.json(11,5): error TS5023: Unknown compiler option 'resolveJsonModule'.
+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error parsing triggers: Cannot find module './serviceAccountKey.json'

Try running "npm install" in your functions directory before deploying.

Answer №1

It is advisable to utilize the require() function, instead of import, when importing the contents of a JSON file. However, if you insist on using import (although not recommended), you can refer to this resource for more information.

Answer №2

After removing the import statement, everything seems to be functioning properly. However, I encounter deployment issues whenever I include the import in my file.

I realized that a couple of important tsconfig options were missing:

"esModuleInterop": true,
"resolveJsonModule": true,

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring TypeScript's type discrimination with objects

When working with TypeScript, type discrimination is a powerful concept: https://www.typescriptlang.org/play#example/discriminate-types But is it possible to achieve something like this? type A = {id: "a", value: boolean}; type B = {id: "b ...

How to properly cast interfaces when using Angular 4's HttpClient to fetch items

Here is the layout of my interface interface IPlacesResult { summary: { queryTime: number; // ... }; results: { id: string; // ... address: { streetNumber: number; // ... }; }[]; } To populate this interface, I ...

What is the syntax for adjusting background-position with ngStyle in Angular 4?

It's finally Friday! I'm a bit confused about how to properly set the background-position-x property in an [ngStyle] directive in Angular 4 with Ionic 3. Can someone guide me on the correct way to implement background-position-x? I expect the ou ...

Is there a way to determine if text can be parsed into a POJO without the need for error handling, Jackson?

Whenever I use ObjectMapper to convert a string to POJO, I find myself consistently enclosing my code in try-catch blocks to ignore any errors that may arise. ObjectMapper mapper = new ObjectMapper(); try { mapper.readValue(body, A.class); } catch (I ...

Extracting information from a Json String using List<t> on an android platform

I am currently extracting data from a Json String using the List<t> method. I have successfully retrieved the Question and QId data, but the Options are nested within another Json string. This is causing some confusion on how to access that speci ...

Acquiring an element through ViewChild() within Angular

I am in need of a table element that is located within a modal. Below is the HTML code for the modal and my attempt to access the data table, which is utilizing primeng. <ng-template #industryModal> <div class="modal-body"> <h4>{{&a ...

Learn how to use Volley to parse JSON data in Java

Previously, I had been creating JSON in PHP using array-data-name in the following manner : print "{data : ".json_encode($the_data,JSON_UNESCAPED_UNICODE)."}" When working with Android Studio in Java, I followed this approach : String url = "httpmywebsi ...

Mastering Typescript Inversify: The Ultimate Guide to Binding Interfaces with Type Parameters

I am trying to figure out how to bind an interface with a type parameter, but I am unsure of the correct way to do it. Here is the Interface: ... export interface ITestHelper<Entity extends ObjectLiteral> { doSomething(builder: SelectQueryBuilder& ...

How to condense individual object arrays in JSON?

Is there a better way to collapse single object arrays in jq here? and for R, how can I collapse only specific elements as discussed here. I am looking to remove the "group" arrays from: [ { "id2": "A", "group": [ { "data": [ ...

Adding optional properties to TypeScript interfaces

As discussed in this post, the optional ? operator is commonly used to indicate that a function parameter can be omitted. But what is the significance of the ? operator when it appears on interface parameters? For instance, consider the following TypeScrip ...

Typescript error message TS2693: The identifier 'Note' is used as a value although it refers to a type

I have been attempting to incorporate Typescript into my Firebase project, but unfortunately I am encountering the following errors: error TS2693: 'Note' only refers to a type, but is being used as a value here. The code snippet I used is as fo ...

A missing Array.at() method has been reported in TypeScript array type

When I try: const myArray = [0,4,2]; myArray.at(-1); I encounter the following error related to .at The error message reads: Property 'at' does not exist on type 'number[]'.ts (2339) Why is Array.at() not working in this case? Is th ...

In my Android app, I am utilizing JsonReader to access a specific value within the current object before moving on to parse an inner object

Here is a snippet of the JSON object I am working with: { id: 1 object: { object_id:1 key_for_1: "value for object type 1" } type: "object_type_1" } { id: 2 object: { object ...

Leveraging process.env with TypeScript

Is there a way to access node environment variables in TypeScript? Whenever I try using process.env.NODE_ENV, I encounter the following error : Property 'NODE_ENV' does not exist on type 'ProcessEnv' I even tried installing @types/no ...

Having trouble with the performance of a kendo UI treeview connected to a JSON

I have successfully implemented a kendo UI tree using an external JSON file. Everything works fine when I have around 200 nodes, but it takes too long when dealing with a large amount of data. You can view the implementation here. Below is the jQuery co ...

Locate a specific item by its ID within a JSON file utilizing Angular version 2 or later

My JSON file structure is like the example below: { "id": "1", "country": "Brazil", "state": [ {"id": "1", "name": "Acre", "city": [ { "id": "1", "name": "Rio Branco"}, { "id": "2", "name": "Xapuri"} ...

Deactivating the drag feature when setting the duration of a new event in FullCalendar

Hello there! I've integrated full calendar into my Angular project and I'm facing a challenge. I want to restrict users from defining the duration of an event by holding click on an empty schedule in the weekly calendar, where each date interval ...

What is the best way to retrieve property names that are not nullable from a type?

I am seeking to create a custom mapped conditional type in TypeScript that will allow me to extract property names from a type, while accounting for properties that can potentially have a value of null. For example: interface Person { name: string a ...

Executing an authenticated Guzzle POST request

Just starting out with guzzle and I need help on how to make a Guzzle call for this specific request. curl -X POST -u username:password -H "Content-Type: application/json" https://xyz/api/v1/accounts.json -d '{"user":{"username":"test","password":"xy ...

Typescript: The Art of Typing Functions

Hey there! I've been diving into learning typescript and have been working through some exercises. If you're curious, you can check out the exercise here. I'm a bit stuck on grasping how to approach this particular example. Here's the ...