There are times when you may wish to share code between two function resources in a Stackery stack. For example, if you have one lambda that populates a database and another that lists database content, you'll want to access database configuration code in both.
To do this, you need to add a line of code to the
package.json file of each lambda function.
Once you've created a stack on Stackery, commit it in order to push it up to GitHub. You can make changes on GitHub or, if you prefer, pull the repository down and modify it in your individual development environment.
Using this repository as an example, we'll look at a case where List Accounts and Seed Database functions (both in /Stackery/functions) need to access configuration settings in the knexfile folder.
First, it's important to note that, just like any other package required via
package.json, this shared package needs:
- to have a
package.jsonto export a module
Now look at the
package.json in the List Accounts folder. Line 6 is the crucial code here:
Basically, this will point the lambda function to the top level of the repository, where it can access the knexfile package.
If you'd like to learn more about working with node packages and
package.json files, check out this guide from the npm docs.