typescript - 如何在 CodeBuild/Ubuntu (TS2307) 中使用 tsc 解析相对路径?
问题描述
我无法在 AWS CodeBuild(Ubuntu 映像)中转换我的打字稿文件,出现 TS2307 错误,无法解析我自己的文件。
当然,我在本地尝试了完全相同的项目。调用tsc
它正在读取项目目录根目录中的我的 tsconfig.json 文件。然后我会运行node ./dist/index.js
以使用节点(而不是ts-node ./src/index.ts
)执行应用程序。有效(REST 服务提供 json 数据)。
在 AWS CodeBuild 中tsc
失败。
这些是我无法相对解析的代码行。在有绝对进口(例如import * as express from 'express'
)之前,它们都可以正常工作。
有人知道为什么它不能在 AWS CodeBuild 中解析/转换,尽管它是相同的项目文件(都是在从 github 拉取之后)?我错过了什么标志?
我在本地使用 Windows。和 CodeBuild 中的 Ubuntu。
import { TermEndpoints } from './endpoints/termEndpoints'
import { TranslationEndpoints } from './endpoints/translationEndpoints'
import { LanguageEndpoints } from './endpoints/LanguageEndpoints'
import { NavLangEndpoints} from './endpoints/navLangEndpoints'
import { InfoEndpoint } from './endpoints/infoEndpoint'
我的 tsconfig.json 文件是
{
"compilerOptions": {
"baseUrl": "./src/",
"outDir": "./dist",
"allowJs": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": false,
"strict": false,
"declaration": true,
"experimentalDecorators": true,
"importHelpers": true,
"esModuleInterop": false,
"resolveJsonModule": true,
"removeComments": true,
"types": ["node"],
"typeRoots": [
"node_modules/@types"
],
"lib": [ "es2017", "dom" ]
},
"include": [
"./src/**/*"
]
}
来自代码构建的日志文件
[Container] 2020/05/11 10:06:10 Running command npm install typescript
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ typescript@3.8.3
updated 1 package and audited 655 packages in 2.484s
found 0 vulnerabilities
[Container] 2020/05/11 10:06:13 Running command tsc --version
Version 3.8.3
[Container] 2020/05/11 10:06:13 Running command npm run build:acc
> sem-translator-api@0.0.1 build:acc /codebuild/output/src400516343/src/github.com/svabra/semtranslatorapi
> tsc
src/ExpressServer.ts(11,31): error TS2307: Cannot find module './endpoints/termEndpoints'.
src/ExpressServer.ts(12,38): error TS2307: Cannot find module './endpoints/translationEndpoints'.
src/ExpressServer.ts(13,35): error TS2307: Cannot find module './endpoints/LanguageEndpoints'.
src/models/relation.ts(3,22): error TS2307: Cannot find module './Term'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! sem-translator-api@0.0.1 build:acc: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the sem-translator-api@0.0.1 build:acc script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-05-11T10_06_18_948Z-debug.log
[Container] 2020/05/11 10:06:18 Command did not exit successfully npm run build:acc exit status 2
[Container] 2020/05/11 10:06:19 Phase complete: BUILD State: FAILED
[Container] 2020/05/11 10:06:19 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm run build:acc. Reason: exit status 2
解决方案
失败原因:菜鸟失误
我只回应其他Windows用户学习(这是对我自己的侮辱,有利于新手)。
该错误是由于 Windows 上不区分大小写。我从不使用大写文件名,但出于某些原因,我曾经为TermEndpoints.ts
(和另一个)做过。在导入小写文件时,termEndpoints.ts
这当然可以在 Windows 10 中使用。无论如何,我之前看到并修复了它 - 但未能将其上游化-u
(git push -u origin master
)。因此,我认为它是固定的。不是。对不起,伙计们挂在那里。
不区分大小写的风险
DevOps 明智的做法可能是一个耗时的惩罚 - 或者 - 如果你错过了在 Linux 上进行端到端测试,这是一个危险的场景。验收测试或生产发布失败。
消除风险:进行区分大小写的检查
如果没有吸取教训,一个人就不是工程师。这是 webpack 的npm 包的保护措施,它强制区分大小写的路径。正是我们需要防止这种风险。这可以应用于 javascript 以及 typescript(在发布之前应该将 typescript 转换为 javascript --> tsc -p .
)
另一种选择是(就像我一样)编写自己的脚本来在部署时测试引用。例如,我正在连接 AWS Elastic Beanstalk .ebextensions。我在 .ebextensions 文件夹中的 .config 文件中调用该命令。当然,还有许多其他解决方案如何调用您的验证脚本。
降低风险:克隆您的生产环境
如果您觉得这永远不会发生在您身上,因为您是在 Linux 或 Max(也不是您的继任者)上进行开发,请确保最迟您的验收测试环境是生产环境的克隆。因为它不仅仅是 Windows 的不区分大小写可以让你。它也是文件系统的大小写保留、unicode 形式保留等。
如今,使用 Docker、AWS Beanstalk、AWS CloudFormation 或 TerraForm(Azure 和 Google 有类似的服务)克隆环境很容易。前者可以通过单击进行克隆,后两个允许您编写基础架构脚本并启动一个新实例以进行集成、验收测试、生产等。确保 DevOps 成功。
推荐阅读
- azure-active-directory - 如何使用 terraform 通过 apim 开发人员门户启用 aad
- python - Condor OutOfMemoryError
- javascript - 如何使用 Blob 在文本文件中添加换行符
- python - 如何根据使用 OpenCv / scikit-image 的图像处理的主要关键术语来确定图像的质量
- editor - 是否有任何免费的、编码友好的文本编辑器,具有类似于 Microsoft-Word 样式导航窗格的功能?
- python - 我正在尝试从网站 https://animaldiversity.org/accounts/Callithrix%20humilis 从
- next.js - 构建 Next.js 项目时出错:无法加载配置“react-app”以扩展自
- bash - Bash Script如何获取第二个属性及其具体值
- node.js - 带有字符 '、" 或 & 的内容可能需要用三个括号进行转义
- php - $_GET 总是给出 1