Can I import typescript types from a Lambda Layer?

泪湿孤枕 提交于 2020-01-14 14:22:07

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/57553131/can-i-import-typescript-types-from-a-lambda-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!