'Node.js lambda: how to import common layer utility lib in local development but also work in the cloud

I have a monorepo with a few functions in their own folder. I want to create a common layer with utility functions that I can require/import locally and also after deployment to the cloud. I want to add a layers/ directory to do this, with the folder structure looking something like this:

  A
  |- node_modules/
  |- package.json
  |- index.js
  B
  |- node_modules/
  |- package.json
  |- index.js
  layers
    |- nodejs
      |- node_modules
      |- package.json
      |- util.js

I'm not set on the folder structure above and am OK with it changing.

Currently, I only have A and B with their own individual dependency layers (orchestrated using terraform), and they work great locally and deployed.

With the introduction of a common utility layer, the issue is that I can require/import locally but it wouldn't be the same in the cloud (/opt/nodejs/*), but if I change it up to work on the cloud, it won't work locally.

I am not using the serverless framework.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source