Share Code Between Services

Link to chapter -

I have tried to share some code in a libs folder outside of the folder where my serverless.yml file is. It works perfectly with sls invoke local but invocations fail once deployed : do I have to specify something in particular in my config so that my libs folder is packaged and included ?

Hmm when you say the invocation fails, whats the command that is failing?

sls invoke --function functionName

I have found what was going wrong :

handler: ../file.default

does not work : handler needs to be in the same subtree as the serverless.yml file.

1 Like

Glad you figured it out.

Thank you for the article, however it seems that something is missing. I am trying to share code in an external folder, but when I deploy it to lambda I get an error that it cannot find the library.

here is the require in the handler.js file
const errors = require('../../utils/errors/errors');

but when I run the code on lambda it says
Cannot find module '../../utils/errors/errors'

I assume, because you mentioned earlier in the article, that you are using serverless-bundle however, in this section you don’t mention it, or how you set it up.

I installed serverless-bundle, and the error has changed to
Cannot set property 'getError' of undefined
that is the name of the function I am calling in the error util. Do you know if serverless-bundle is moving the file, so I need to use a different path in the require?

Anyways there seems to be more complexity to including shared libraries than the original article indicates.


That plugin supports the import command instead. Have you tried using that?

I am importing the custom section in the shared serverless.yml as described:

custom: ${file(../../serverless.common.yml):custom}

But in a particular service I want to set some more properties in the custom section. How can I both import the file and add to the custom section? When I try this (and many other variations), I get various errors:

        - rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux --target=10.15.0 sharp

Hmm yeah, I haven’t done this before. I’ll need to try somethings out and see.