How to include js function with import in firebase cloud functions?

I'm writing firebase cloud function:

const {getContactObject} = require('../../../../src/modules/Contacts/scenes/Contactlist/ContactsManager/functions/getContactObject')

const getApiResponsible = require('../../functions/getApiResponsible')

const createContact = async payload => {
  console.log('payload', payload)
  console.log(getContactObject(getApiResponsible()))
}

module.exports = createContact

My function with name getContactObject is located in src folder of the project, and its using es6 import/export

getContactObject.js

import { getCurDateWithUser } from '../../../../../../utilities/date/getCurDateWithUser'

export const getContactObject = uid => {
  return {
    lastName: '',
    name: '',
    middleName: '',
    gender: '',
    phone: [],
    email: [],
    messangers: [],
    social: [],
    address: [],
    dates: [],
    leads: [],
    sales: [],
    responsible: '',
    created: getCurDateWithUser(uid),
    updated: getCurDateWithUser(uid),
  }
}

How can i use it in my firebase cloud function, that's using node js 8?

Is it possible to import getContactObject function without rewriting it?

Now i'm having errors about imports:

Errors log

Upvotes: 2

Views: 898

Answers (1)

ajorquera
ajorquera

Reputation: 1309

You have two options:

  1. Rewrite the following line:
import { getCurDateWithUser } from '../../../../../../utilities/date/getCurDateWithUser'

to

const getCurDateWithUser = require('../../../../../../utilities/date/getCurDateWithUser')
  1. Use Typescript. In your tsconfig.json make sure to set
{
  //...
  "compilerOptions": {
        //..
        "module": "commonjs"
  }
} 

Upvotes: 1

Related Questions