Using require to include a relative module in Netlify Functions on Node

I did get some success (locally & live) in putting shared modules in a local npm package:

/functions
  /utils
    package.json
    index.js
  /src
    /auth
      auth.js
    /trails
      trails.js
  package.json

Export all common modules in functions/utils/index.js and set property "main": "index.js" in functions/utils/package.json.

In functions/package.json install the module:

{
  "dependencies": {
    ...
    "utils": "file:utils"
  }
}

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.

2 Likes