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

使用TypeScript的CDK - 在Cloud9上出现错误。

0

【以下的问题经过翻译处理】 我在Cloud9环境中尝试使用 https://github.com/fortejas/example-serverless-python-api,但是出现了以下错误。

我使用的设置命令如下:

mkdir sample-api
cd sample-api/
cdk init app --language typescript .
cd ~
git clone https://github.com/kasukur/example-serverless-python-api.git
ls -lrt example-serverless-python-api/
cp -rf example-serverless-python-api/lambda-api/ ~/environment/sample-api/.
cd ~/environment/sample-api/
Delete node_modules folder
Delete package-lock.json
npm i @aws-cdk/aws-lambda-python-alpha --force -g

ec2-user:~/environment/sample-api  $ cdk deploy

错误为


ec2-user:~/environment/sample-api  $ cdk synth
npm WARN exec The following package was not found and will be installed: ts-node
/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:750
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
bin/sample-api.ts:4:10 - error TS2305: Module '"../lib/sample-api-stack"' has no exported member 'SampleApiStack'.

4 import { SampleApiStack } from '../lib/sample-api-stack';
~~~~~~~~~~~~~~

at createTSError (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:750:12)
at reportTSError (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:754:19)
at getOutput (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:941:36)
at Object.compile (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1243:30)
at Module.m._compile (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1370:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/home/ec2-user/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1374:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
diagnosticText: `\x1B[96mbin/sample-api.ts\x1B[0m:\x1B[93m4\x1B[0m:\x1B[93m10\x1B[0m - \x1B[91merror\x1B[0m\x1B[90m TS2305: \x1B[0mModule '"../lib/sample-api-stack"' has no exported member 'SampleApiStack'.\n` +
'\n' +
"\x1B[7m4\x1B[0m import { SampleApiStack } from '../lib/sample-api-stack';\n" +
'\x1B[7m \x1B[0m \x1B[91m ~~~~~~~~~~~~~~\x1B[0m\n',
diagnosticCodes: [ 2305 ]
}
Subprocess exited with error 1

有人能帮忙看看这是怎么回事吗? 谢谢!

profile picture
专家
已提问 1 年前41 查看次数
1 回答
0

【以下的回答经过翻译处理】 模块“' ../lib/sample-api-stack '”没有导出成员' SampleApiStack'”,这是出现问题的根本原因。 https://github.com/fortejas/example-serverless-python-api/blob/main/lib/sample-api-stack.ts 导出ServerlessApiStack而不是SampleApiStack

CDK行为解释:

  • cdk.json 定义了编译CDK应用程序的命令:"app":"npx ts-node --prefer-ts-exts bin/sample-api.ts"
  • bin/sample-api.ts 由CDK init命令生成以下导入:import { SampleApiStack } from '../lib/sample-api-stack';
  • 你用自定义文件替换了生成的文件,该文件导出了不同的类
  • TypeScript在编译你的代码时出现错误,CDK出现上述错误。
profile picture
专家
已回答 1 年前

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

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

回答问题的准则