I have 3 separate functions each in their own folders. All of them make use of a Twilio client and Apollo Client for dealing with SMS and GraphQL server respectively.
Rather than having all the code to instantiate each client (get keys from env etc.) in each file, can it be put somewhere and required in?
I've tried putting the code into a .js file in the top level functions/ folder and requiring it in the function code as below and this works fine locally on netlify dev but errors with Module not found '../twilioClient' when the function is called in live environment.
/functions
apolloClient.js
twilioClient.js
package.json - specifying deps used by above files
/auth
auth.js - require('../apolloClient')
...
/trails
trails.js - require('../twilioClient') etc.
...
I did get some success (locally & live) in putting shared modules in a local npm package:
Export all common modules in
functions/utils/index.jsand set property"main": "index.js"infunctions/utils/package.json.In
functions/package.jsoninstall the module:And import it in your functions (in
functions/src/auth/auth.js):import { apolloClient, twilioClient } from "utils"Please take a look at this repository for reference.