Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I import typescript types from a Lambda Layer?

I am attempting to use TypeScript when building Lambda functions but hitting an issue when using a Lambda Layer which is also written in TypeScript.

TypeScript does not recognise the /opt/nodejs/... import for my Layer (as it would running in SAM or AWS) and therefore I am unable to import types which I have defined in the Layer to my Lambda function.

I have tried looking to see if I am able to somehow npm link the layer but I can't seem to get that working because the import is a local path '/opt/nodejs...' and not a just a module name.

Layer:

export interface SomeType {
  someField: string
}

Lambda:

import { SomeType } from '/opt/nodejs/myLayer' // this does not work

I am just getting the error: Cannot find module '/opt/nodejs/myLayer'.ts(2307) and I find myself having to suppress this with // @ts-ignore can be ignored as this is a Lambda layer but that means I am unable to use the TypeScript types from the layer.

like image 425
user3067870 Avatar asked Aug 19 '19 08:08

user3067870


1 Answers

I think I may have solved this one, I added the /opt/nodejs... path as a path mapping in tsconfig.json:

{
    "paths": {
      "/opt/nodejs/myLayer": ["../../layers/myLayer/src/some-layer-module"]
    }
}

I can now import TypeScript types from the Layer and as the import path isn't changed, it will still work in AWS/SAM

like image 65
user3067870 Avatar answered Sep 28 '22 02:09

user3067870