使用AWS re:Post即您表示您同意 AWS re:Post 使用条款

relative import resolution error with transpiled TypeScript code in Lambda with container images

0

I'm trying to get started developing a lambda function via typescript.

I've following the instructions here to set up my project: https://docs.aws.amazon.com/lambda/latest/dg/typescript-image.html

The example code given at the link above builds and runs just fine.

But when I modify the code as follows, the build step fails:

  1. I add a file schema.ts in the project's root directory that exports an object like this:
export const ValidRequestObject = {sessionId: 2}
  1. In index.ts I add `import { ValidRequestObject } from './schema'.

Running docker build --platform linux/amd64 -t docker-image:test . now fails. Here's the error:

0.730 ✘ [ERROR] Could not resolve "./schema"
0.730
0.730     index.ts:2:30:
0.730       2 │ import { ValidRequestObject } from './schema';
0.730         ╵                               ~~~~~~~~~~
0.730

To be clear, the above are the ONLY modifications I've made to the example in the documentation linked above.

So I guess the question is: What do I need to do to make relative imports work at the build step?

已提问 5 个月前271 查看次数
1 回答
0

So the dockerfile suggested by the aws documentation I followed has the line

COPY package.json index.ts ./

I would expect that the ./ part of this would cause every file in the project directory to be copied over to the container.

Apparently not. The problem was fixed by manually adding the file from which index.ts was importing to that line like so:

COPY package.json index.ts schema.ts ./

Although this fixes the problem, I'm still not sure how to cause all the files in the project directory (excepting of course .gitignore) to get copied over to the container before the build step without having to manually write their names into the COPY line as above. If anyone knows, please tell me.

已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则