# Share Code Between Services

**URL:** <https://discourse.sst.dev/t/share-code-between-services/1333>\
**Category:** Chapter Comments\
**Created:** [October 9, 2019, 2:05am UTC](https://discourse.sst.dev/t/share-code-between-services/1333 "2019-10-09T02:05:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [October 9, 2019, 2:05am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/1 "2019-10-09T02:05:27Z")

</div>

Link to chapter - [https://serverless-stack.com/chapters/share-code-between-services.html](https://serverless-stack.com/chapters/share-code-between-services.html)

---

<div class="post-metadata">

**Author:** ![aymericbouzy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/aymericbouzy/32/700_2.png) [@aymericbouzy](https://discourse.sst.dev/u/aymericbouzy)\
**Post date:** [December 11, 2019, 2:15pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/2 "2019-12-11T14:15:48Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [December 16, 2019, 1:35am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/3 "2019-12-16T01:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aymericbouzy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/aymericbouzy/32/700_2.png) [@aymericbouzy](https://discourse.sst.dev/u/aymericbouzy)\
**Post date:** [December 16, 2019, 8:29am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/4 "2019-12-16T08:29:10Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 12, 2020, 2:56am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/5 "2020-01-12T02:56:10Z")

</div>

Glad you figured it out.

---

<div class="post-metadata">

**Author:** ![rmclaughlin](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@rmclaughlin](https://discourse.sst.dev/u/rmclaughlin)\
**Post date:** [January 15, 2020, 4:54pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/6 "2020-01-15T16:54:28Z")

</div>

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.

Thanks

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 26, 2020, 6:16pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/7 "2020-01-26T18:16:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Susan](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Susan](https://discourse.sst.dev/u/Susan)\
**Post date:** [February 14, 2020, 9:42pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/8 "2020-02-14T21:42:19Z")

</div>

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

```auto
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:

```auto
custom:
  ${file(../../serverless.common.yml):custom}
  bundle:
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux --target=10.15.0 sharp

```

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [February 23, 2020, 3:51am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/9 "2020-02-23T03:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/aaron/32/885_2.png) [@Aaron](https://discourse.sst.dev/u/Aaron)\
**Post date:** [April 11, 2020, 12:20am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/10 "2020-04-11T00:20:37Z")

</div>

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

and getting the error:

```auto
{
    "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:

```auto
import config from "../config";

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

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

```

Any idea what the issue could be?

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/aaron/32/885_2.png) [@Aaron](https://discourse.sst.dev/u/Aaron)\
**Post date:** [April 11, 2020, 1:38am UTC](https://discourse.sst.dev/t/share-code-between-services/1333/11 "2020-04-11T01:38:20Z")

</div>

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:

> [@Add a Get Note API](https://discourse.sst.dev/t/add-a-get-note-api/132/60):
>
> I am getting the following error. I have checked and checked I cannot see what I did wrong. I can create an event just cant get it. C:\Users\activated\myfitnessbook-app-api\>serverless invoke local --function create --path mocks\get-event.json Serverless: Bundling with Webpack... Time: 620ms Built at: 07/07/2019 9:04:46 PM Asset Size Chunks Chunk Names create.js 10.1 KiB create [emitted] create create.js.map 7.23 KiB create [emitted] create Entrypoint create…

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.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [April 13, 2020, 8:11pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/12 "2020-04-13T20:11:51Z")

</div>

Ah glad you figured it out. Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![GioAgu17](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/gioagu17/32/1098_2.png) [@GioAgu17](https://discourse.sst.dev/u/GioAgu17)\
**Post date:** [March 1, 2021, 5:49pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/13 "2021-03-01T17:49:31Z")

</div>

How are you thinking to fix the issue described [here](https://forum.serverless.com/t/variables-resolver-deprecation-cannot-load-file-from-outside-of-service-folder/14185) for using the variable custom? Any suggestion would be appreciated

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [May 15, 2021, 10:48pm UTC](https://discourse.sst.dev/t/share-code-between-services/1333/14 "2021-05-15T22:48:09Z")

</div>

From reading that thread, is that still an issue?
