Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless framework and path mapping from tsconfig.json don't work

I have a path mapping defined in the tsconfig.json file like this

"paths": {
      "~/*": ["./src/*"]
    }

and it works with ts-node -r tsconfig-paths/register, but when it comes to lambdas that are compiled from .ts to .js files with the help of serverless-plugin-typescript plugin, it doesn't work.

I can't do this inside my lambdas import * as db from '~/db/psql'; any more.

I know why and that's ok, but what can I do to make it work.

I'm trying to switch to serverless-plugin-typescript to compile .ts files to .js files. Before, I was using webpack with tsconfig-paths-webpack-plugin and that worked, but I would like to avoid webpack for compiling lambdas.

like image 667
Petar Popovic Avatar asked Oct 25 '25 00:10

Petar Popovic


1 Answers

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

2) Utilize Webpack plugin to override servlesss and 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

like image 81
Colleen Grafton Avatar answered Oct 28 '25 02:10

Colleen Grafton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!