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
1 Like

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

I am running the the following command:
serverless invoke local --function get --path mocks/get-event.json

and getting the error:

    "errorMessage": "Unexpected token u in JSON at position 0",
    "errorType": "SyntaxError",
    "stackTrace": [
        "SyntaxError: Unexpected token u in JSON at position 0",
        "    at JSON.parse (<anonymous>)",
        "    at main ([...]/notes-ext-api/services/notes-api/.webpack/create/webpack:[...]/notes-ext-api/services/notes-api/create.js:6:21)",
        "    ...

It goes on for a while but when I invoke the create function and the update functions locally, they work fine. But when I run either get or list functions I get this error.

I’m not sure why it is referencing create.js when I am calling the get function. Here is my dynamodb-lib.js file:

import config from "../config";

const dynamoDb = new AWS.DynamoDB.DocumentClient();

export function call(action, params) {
  // Parameterize table names with stage name
  return dynamoDb[action]({
    TableName: `${config.resourcesStage}-${params.TableName}`

Any idea what the issue could be?

So… this is awkward. After scouring this forum for clues I found a lot of people having the same problem but they seemed to abandon the discussion before getting to any resolution. It wasn’t until I read this comment from @ActivateDza:

that I finally realized what I was doing wrong.

I’m going to document it here so it can help others in the future.

If you look at the serverless invoke local call above, the function passed in is create but the event.json object passed in is the get-event. So in my haste to test all these new apis I was using the up arrow in the terminal to recall the last command and editing the name of the event object but not the function. Once I fixed that all the tests passed.

1 Like

Ah glad you figured it out. Thanks for sharing!

1 Like

How are you thinking to fix the issue described here for using the variable custom? Any suggestion would be appreciated

From reading that thread, is that still an issue?