问题
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