使用 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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南