Using local libs

I had made another post about my specific situation, which didn’t get any replies. Well, I’m stuck with the same issue a week later. I will make this one more general.

What is the recommended best practice for using local libraries? My file structure currently looks like this:


How do I include the twilioClient library to be user in service1 and service 2? Currently the only working solution I have is to have a copy in each service, but obviously that is a huge pain because for every change I make to the code, I will have to update it multiple times.

I used npm install file: which worked for the inclusion, but I am using babel and webpack, which excluding my node_modules folder, as recommended. I tried any solutions I could find, such as: exclude: /node_modules\/(?!(twilioClient))/ but I get a syntax error on es6 code every time.

Any help would be greatly appreciated, as I assume this is a common use case.

I’m facing the same problem and could not find a best practice for this. What I did so far as a workaround: I wrote a small script, that builds the libraries, packs them and installs those packsages as dependencies to the services.

Manual example with your structure:

1. Build twilioClient (you're using babel, I am using TypeScript) ("npm run build")
2. Pack your library with `npm pack`. This will create a *.tgz file (probably `twilioClient-1.0.0.tgz`).
3. Install library to service: `cd service1 && npm install --save ../my-local-libs/twilioClient/twilioClient-1.0.0.tgz`

Now you can use your libary inside of your service1 by using require / import statements.

My small script just does this for my whole project, i.e. building and packaging my libraries (sometimes install one lib to another) and afterwards install all the libs into the service, which are dependent on them. Of course I have to do this everytime I change some code in my libs, but the scripts takes only a couple of seconds. Best way I could find so far.

Also I’m using npm pack instead of npm install because the former one will exclude all unnecessary stuff (like devDeps, src code etc). This was important for our project since we’re using TypeScript and the tsc is huge (like 30MB). Note, that npm5 only creates a symlink and serverless does not seem to be able to remove devDeps from symlinks. Otherwise our services would have been to big for Lambda.

There exists a related issue on github: