简体   繁体   中英

Firebase Admin Nodejs cannot access a .json file in AWS Lambda layer

I am trying to use FireStore-admin in NodeJS in AWS Lambda using layers. For this, I have zipped the node_modules and serviceAccount.json file in the following hierarchy and uploaded it to the layer:

nodejs->

  • node_modules
  • package.json
  • package-lock.json
  • serviceAccount.json (contains credentials to connect to FireStore collection)

The code is as follows:

var admin = require('firebase-admin');
var serviceAccount = require('./serviceAccount.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

But I receive the following error when I run this lambda function:

"errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module './serviceAccount.json'

You can also find the image of the zip attached below:

等级制度

How do I access the json file and/or correct the hierarchy to get this working?

I think this should work so long as your file where this code lies is in the same directory as serviceAccount.json

import * as serviceAccount from './serviceAccount.json'
var admin = require('firebase-admin');    
    
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM