# Folder structure for multiple servies with shared library?

**URL:** <https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153>\
**Category:** Serverless Framework\
**Created:** [January 20, 2017, 5:53am UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153 "2017-01-20T05:53:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkristia](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jkristia](https://forum.serverless.com/u/jkristia)\
**Post date:** [January 20, 2017, 5:53am UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/1 "2017-01-20T05:53:00Z")

</div>

Lets say I have

```
-shared
    filea.js
-service1
     service.yml
-service2
     service.yml

```

Each service uses the same shared library.  
In TypeScript I pull in the service using

```
 `import { ... } from '../shared'`

```

I have included the files in the yml include section.  
The problem is that in the packed zip file the structure has changed to

```
-shared
service1.js

```

and service1 is then unable to require the library files.  
What is the recommended structure for a case with a shared library ?

Edit: One option I can think of is to use webpack or similar to create a single file bundle.

Thanks

---

<div class="post-metadata">

**Author:** ![mobiyan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mobiyan/32/412_2.png) [@mobiyan](https://forum.serverless.com/u/mobiyan)\
**Post date:** [January 21, 2017, 5:02pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/2 "2017-01-21T17:02:41Z")

</div>

Have you solved this? I am looking at sharing code between my functions and can’t make it work.

---

<div class="post-metadata">

**Author:** ![jkristia](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jkristia](https://forum.serverless.com/u/jkristia)\
**Post date:** [January 21, 2017, 5:24pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/3 "2017-01-21T17:24:45Z")

</div>

I ended up using webpack to create a bundle per function. When using webpack set the library target to commonjs2.

```
	entry: {
		// save bundle is source location, this is required for serverless.yml
		'./server/webpackbundle1': path.join(__dirname, './server/test1'),
		'./server/webpackbundle2': path.join(__dirname, './server/test2'),
	},
	// use commonjs2 as output when creating a lambda bundle
	output: {
		libraryTarget: 'commonjs2',
		filename: '[name].js'
	},
	target: 'node',

```

then in yml I’m setting the function handler to e.g. webpackbundle1.handler.

I’m not sure this is the best approach, but at least it works for what I need.

---

<div class="post-metadata">

**Author:** ![mobiyan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/mobiyan/32/412_2.png) [@mobiyan](https://forum.serverless.com/u/mobiyan)\
**Post date:** [January 21, 2017, 5:34pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/4 "2017-01-21T17:34:45Z")

</div>

Ok thanks, I will give it a try if I can’t find another solution. Have you tried this module: serverless-shared-library?

---

<div class="post-metadata">

**Author:** ![jkristia](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jkristia](https://forum.serverless.com/u/jkristia)\
**Post date:** [January 21, 2017, 6:11pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/5 "2017-01-21T18:11:16Z")

</div>

no I’m not aware of that module. If you do find another solution, maybe you can post it on this forum?

---

<div class="post-metadata">

**Author:** ![contractorwolf](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/contractorwolf/32/1624_2.png) [@contractorwolf](https://forum.serverless.com/u/contractorwolf)\
**Post date:** [January 23, 2017, 3:56pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/6 "2017-01-23T15:56:50Z")

</div>

I just use simple syntax like this to include another file:  
var routehelper = require(’…/routes/routehelpers’);

Does that not work for you?

---

<div class="post-metadata">

**Author:** ![jkristia](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jkristia](https://forum.serverless.com/u/jkristia)\
**Post date:** [January 23, 2017, 4:49pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/7 "2017-01-23T16:49:45Z")

</div>

I sure it will. I was no aware I can specify a path in the lambda handler, and without that the handler is placed at root level, meaning you cannot include files above.  
If instead you change the folder structure to something like this

```
dist
    shared
        *.js
    serviceA
        fn1handler.js
        fn2handler.js
    serviceB
        handler.js
src
    shared
        *.js
    serviceA
        handler.js
        serverless.yml
    serviceB
        handler.js
        serverless.yml

```

and then define the handlers as

```
functions:
  fn1:
    handler: 'dist/serviceA/fn1handler.handler'
  fn2:
    handler: 'dist/serviceA/fn2handler.handler'

```

Then I’m sure it will work

---

<div class="post-metadata">

**Author:** ![polvoazul](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/polvoazul/32/103_2.png) [@polvoazul](https://forum.serverless.com/u/polvoazul)\
**Post date:** [January 25, 2017, 4:04pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/8 "2017-01-25T16:04:19Z")

</div>

I solved this problem in a simple way: using symbolic links!

```
-common
  -libfile
-service1
  -file1
  -file2
  -libfile -> ../common/libfile
-service2
  -file3
  -file4
  -libfile -> ../common/libfile

```

* * *

Then, i just import libfile as usual inside any of the native services’ file and it works! When serverless zips the services it follows the links and actually creates a normal file, it works great.

---

<div class="post-metadata">

**Author:** ![dmor](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dmor](https://forum.serverless.com/u/dmor)\
**Post date:** [May 13, 2018, 5:17am UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/9 "2018-05-13T05:17:52Z")

</div>

@polvoazul excellent solution, thank you!

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [May 13, 2018, 12:36pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/10 "2018-05-13T12:36:39Z")

</div>

Here is my solution.

add below lines in `pacakge.json`

```
  "dependencies": {
    "common": "file:../common",
   ...
  }

```

The local package `common` will be copied to `./node-modules/common` as other npm modules, when you run `npm install`.

Then you should be fine to reference the shared library as other npm modules

`const common = require("common");`

Reference: [https://docs.npmjs.com/files/package.json#local-paths](https://docs.npmjs.com/files/package.json#local-paths)

---

<div class="post-metadata">

**Author:** ![appzone](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/appzone/32/2206_2.png) [@appzone](https://forum.serverless.com/u/appzone)\
**Post date:** [August 13, 2018, 12:39pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/11 "2018-08-13T12:39:12Z")

</div>

how you put the symbolic link?

---

<div class="post-metadata">

**Author:** ![parifuture](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@parifuture](https://forum.serverless.com/u/parifuture)\
**Post date:** [February 25, 2019, 6:57pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/12 "2019-02-25T18:57:56Z")

</div>

I usually like to stay away from symbolic links. What is wrong with creating a shared folder and adding all the common code there? trying to figure out if symbolic links are serving a specific purpose?

---

<div class="post-metadata">

**Author:** ![parifuture](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@parifuture](https://forum.serverless.com/u/parifuture)\
**Post date:** [February 25, 2019, 6:58pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/13 "2019-02-25T18:58:59Z")

</div>

> ln -s

> **[How to Create and Use Symbolic Links (Symlinks) on a Mac](https://www.howtogeek.com/297721/how-to-create-and-use-symbolic-links-aka-symlinks-on-a-mac/)**
>
> Use symbolic links to store application files elsewhere without breaking the app that depends on them.

---

<div class="post-metadata">

**Author:** ![ericmaicon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ericmaicon/32/3088_2.png) [@ericmaicon](https://forum.serverless.com/u/ericmaicon)\
**Post date:** [February 27, 2019, 12:38pm UTC](https://forum.serverless.com/t/folder-structure-for-multiple-servies-with-shared-library/1153/14 "2019-02-27T12:38:42Z")

</div>

Hey there,

how you managed to get serverless.yml working with the webpack outside of the folder?!  
Do you have a main serverless.yml? How do you shared common config between them?

Also, how are you running the serverless-offline plugin?  
I have a similar architecture, but using TS…
