如何在CodeBuildUbuntu(TS2307)中使用tsc解析相对路径?

问题描述 投票:1回答:1

我在AWS CodeBuild (Ubuntu镜像)中转置我的typescript文件时,出现TS2307错误,无法解析我自己的文件。

当然,我在本地尝试了完全相同的项目。调用 tsc 读取我在项目目录根目录下的 tsconfig.json 文件。然后我将运行 node ./dist/index.js 用node来执行应用程序(而不是用 ts-node ./src/index.ts). 工作(REST服务提供json数据)。

在AWS CodeBuild中的 tsc 失败。

这些是我的代码行,相对来说是无法解析的。之前有绝对导入(如 import * as express from 'express'),都能正常工作。

有人知道为什么在AWS CodeBuild中不能解析ranspile,尽管它是相同的项目文件(都是从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: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
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

> [email protected] 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! [email protected] build:acc: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] 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
typescript aws-codebuild tsc
1个回答
1
投票

失败原因:一个新手的错误。

我的回复只是为了让其他Windows用户学习(这是对我自己的羞辱,有利于菜鸟)。

有错误是由于windows的大小写不敏感造成的。我从不使用大写的文件名,但由于某些原因,我曾经使用过大写的文件名。TermEndpoints.ts 和另一个)。在导入文件时,用小写 termEndpoints.ts 这当然在Windows 10中工作。无论如何,我早些时候看到了它,并修复了它 - 但未能上行 -u 它 (git push -u origin master). 因此我以为它是固定的。其实不然。对不起,你们坚持到现在。

对案件不敏感的风险

从DevOps角度来说,这可能是一个耗时的惩罚--或者--如果你错过了linux上的end2end测试,那将是一个危险的场景。验收测试或生产发布失败。

消除风险:进行案例敏感性检查。

如果没有经验教训,就不是工程师。这里有一个保险箱 npm包 的webpack,它强制执行大小写敏感的路径。这正是我们需要防止这种风险的地方。这可以应用于javascript和typescript (在发布--&gt.之前,我们应该先把typescript移植到javascript中)。tsc -p .)

另一种选择是(像我一样)在部署时写自己的脚本来测试引用。例如,我挂入AWS Elastic Beanstalk的.ebextensions。我在.ebextensions文件夹内的.config文件中调用该命令。Of cource,有几十种其他的解决方案如何调用你的验证脚本。

降低风险:克隆你的生产环境。

如果你觉得这种情况永远不会发生在你身上,因为你是在Linux或Max上开发的(也不会发生在你的后继者身上),请确保你的验收测试环境最迟是生产环境的克隆。因为这不仅仅是Windows的大小写不敏感会让你付出代价。还有文件系统的大小写保存,unicode形式保存等等。

现在用Docker、AWS Beanstalk、AWS CloudFormation或TerraForm(Azure和Google有类似的服务)克隆环境很容易。前者可以通过点击来克隆,后两者允许你为你的基础架构编写脚本,并旋转出一个新的实例,用于集成、验收测试、生产,你说了算。确保DevOps的成功。

© www.soinside.com 2019 - 2024. All rights reserved.