File '../foo.ts' is not under rootDir. rootDir is expected to contain all source files

I have a serverless app that is composed by 3 apis. They share a large amount of code. I prepared the whole application in a way that is allows me to boot every api solely or every single one of them at the same time, regarding my needs for that moment. There are some scripts to deploy the apis separately and all in once, as well.

The problem here is the shared code. As it is shared, I needed to copy them through all apis to maintain it correct, because I can’t have a folder with that code outside any of the apis. It throws the error I put in the title.

My folder structure is this one:

src
|- api1
   |- serverless.yml (for api1)
|- api2
   |- serverless.yml (for api2)
|- api3
   |-serverless.yml (for api3)
|- common_code_1
|- common_code_2

I registered those 2 folders as paths in my tsconfig.json, but it throws the error anyway. How can I share this code between apis without duplicating it?

Hello! Were you able to solve this issue? I am having the same problem.

Hi!

Actually no. I did something I didn’t want to as a workaround: copied all the common code between the 3 folders. I had no time, since then, to try again and find a solution for this, but, once I do, I’ll try to post it here.

Hi, we are using for this Lerna - monorepo package manager.

Hi all, after much digging I found out that serverless does not support the pathing options offered by typescript.

For anyone else it appears the options are:

  1. Copy the code as http://forum.serverless.com/u/GunterJameda suggested

  2. Utilize Webpack to allow setting the rootDir in typescript

  3. Symlink your shared module code to the each of your microservices directories. I wrote a bash script that will symlink a directory called “_shared” to all directories one level under a parent directory called “microservices”:

     `find microservices ! -path microservices -type d -maxdepth 1 -d -exec ln -s ../../_shared ./{} \;`
    
      You'll then want to add each symlink to your .gitignore:
    
      `/microservices/**/_shared`
    
      So the workflow for the project is:
    
         1.First time you clone the repo, run the bash script to create the symlinks
    
         2. When you are in a microservices sub one level directory, reference imports from _shared 
         from the symlink on that same level
    
         3. Make and commit all changes to the top level _shared directory
    

or 4. Use Lerna as http://forum.serverless.com/u/FilipPyrek, although afaik that is only a solution for external dependencies via yarn or npm, and not internal shared modules

What do you mean by “solution for external dependencies” @cgrafton ? One of main features of Lerna is handling internal modules dependencies. https://github.com/lerna/lerna#about

Have you succeeded with Option 2 - Utilize Webpack to allow setting the rootDir in typescript ?
Or maybe have an example?
Thank you!