AWS CDK:找不到模块 'typescript'

AWS CDK: Cannot find module 'typescript'

长话短说,我有一个已经使用了将近一年的单声道存储库。我的硬盘出现故障,不得不重新安装我的 OS。现在,当我尝试使用 CDK 执行任何操作时,每次都会出现此错误。

Cannot find module 'typescript'
Require stack:
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/index.js
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/repl.js
- /home/jpsimkins/.npm/_npx/16449/lib/node_modules/ts-node/dist/bin.js

这只会影响 cdk synth|diff|deploy 命令。我对 运行ning 测试没有任何问题,这让我在检查快照时更加困惑。

起初,我以为这与 PATH 有关,但我不确定为什么现在这会成为问题。我确实使用了 .husky 但后来用我自己的系统替换了它。我只是提到这个,以防万一这可能是值得了解的东西。

这可能是 node_module 分辨率的问题,因为我在此项目和父项目中有一个 node_modules 目录。我正在使用 lerna 的单声道存储库。这不是 lerna 问题,因为此问题仅在 运行ning cdk synth|diff|deploy 时发生。仅创建存储桶的简单测试将引发该错误。

我倾向于 node_modules 决议。这不是我的 tsconfig 所在的 运行ning,因为所有其他脚本都正常工作。我不确定为什么只有 CDK 有这个问题。我已经用其他 4 个二进制文件(jest、prettier、eslint 和 tsc)进行了测试,它们 运行 很好。问题仅存在于 cdk.

我正在使用 Lerna 作为单声道存储库。

这是我的开发依赖项:

  "devDependencies": {
    "@aws-cdk/assert": "1.117.0",
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "@typescript-eslint/eslint-plugin": "^4.4.0",
    "@typescript-eslint/parser": "^4.4.0",
    "aws-cdk": "1.117.0",
    "eslint": "7.19.0",
    "eslint-plugin-jest": "24.3.1",
    "eslint-plugin-import": "2.22.1",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-config-airbnb-typescript": "^9.0.0",
    "eslint-config-prettier": "7.2.0",
    "eslint-plugin-prettier": "3.3.1",
    "eslint-plugin-simple-import-sort": "6.0.1",
    "execa": "^5.0.0",
    "git-url-parse": "^11.4.4",
    "jest": "^26.4.2",
    "jest-extended": "^0.11.5",
    "listr": "^0.14.3",
    "lerna": "^4.0.0",
    "lerna-update-wizard": "^0.17.8",
    "lint-staged": "^10.5.4",
    "prettier": "^2.2.1",
    "ts-jest": "^26.2.0",
    "ts-node": "^9.0.0",
    "typedoc": "^0.20.25",
    "typescript": "~3.9.7",
    "util": "^0.12.3"
  },

我全局安装了 Typescript 以查看是否有帮助(我通常对所有内容都使用本地安装)但没有。

我还通过 运行ning tsc --showConfig:

确认了 tsc 配置是我所期望的
{
    "compilerOptions": {
        "alwaysStrict": true,
        "declaration": true,
        "experimentalDecorators": true,
        "lib": [
            "es2018"
        ],
        "module": "commonjs",
        "noFallthroughCasesInSwitch": false,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "inlineSourceMap": true,
        "inlineSources": true,
        "removeComments": false,
        "strict": true,
        "strictNullChecks": true,
        "strictPropertyInitialization": false,
        "target": "es2018",
        "typeRoots": [
            "/home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/node_modules/@types"
        ],
        "rootDir": "./src",
        "outDir": "./dist"
    },
    "files": [
        "./src/index.ts",
        "./src/stacks/pipeline-test/index.ts",
        "./src/stacks/pipeline-test/src/pipeline-base.ts",
        "./src/stacks/pipeline-test/src/pipeline-project.ts",
        "./src/stacks/s3-replication-sources/index.ts",
        "./src/stacks/s3-replication-sources/src/s3-replication-sources.ts",
        "./src/stacks/test-base/index.ts",
        "./src/stacks/test-base/src/index.ts",
        "./src/stacks/test-web-bucket/index.ts",
        "./src/stacks/test-web-bucket/src/main.ts"
    ],
    "include": [
        "./src"
    ],
    "exclude": [
        "dist"
    ]
}

npm 日志:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/node',
1 verbose cli   '/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/npm',
1 verbose cli   'run',
1 verbose cli   'TestBase:diff'
1 verbose cli ]
2 info using npm@6.14.14
3 info using node@v14.17.4
4 verbose run-script [ 'preTestBase:diff', 'TestBase:diff', 'postTestBase:diff' ]
5 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~preTestBase:diff: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
6 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
7 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: unsafe-perm in lifecycle true
8 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: PATH: /home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1/node_modules/.bin:/home/jpsimkins/.local/bin:/home/jpsimkins/bin:/home/jpsimkins/.nvm/versions/node/v14.17.4/bin:/home/jpsimkins/.local/bin:/home/jpsimkins/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
9 verbose lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: CWD: /home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1
10 silly lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Args: [
10 silly lifecycle   '-c',
10 silly lifecycle   'aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase'
10 silly lifecycle ]
11 silly lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Returned: code: 1  signal: null
12 info lifecycle @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8~TestBase:diff: Failed to exec TestBase:diff script
13 verbose stack Error: @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff: `aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:400:28)
13 verbose stack     at ChildProcess.<anonymous> (/home/jpsimkins/.nvm/versions/node/v14.17.4/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1055:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8
15 verbose cwd /home/jpsimkins/DevOps/oly-iac-mono-repo/aws/development/accounts/olympusat-development-dev/us-east-1
16 verbose Linux 5.11.0-25-generic
17 verbose argv "/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/node" "/home/jpsimkins/.nvm/versions/node/v14.17.4/bin/npm" "run" "TestBase:diff"
18 verbose node v14.17.4
19 verbose npm  v6.14.14
20 error code ELIFECYCLE
21 error errno 1
22 error @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff: `aws2-wrap --profile $(basename $(dirname $(pwd))) ../../../node_modules/.bin/cdk diff TestBase`
22 error Exit status 1
23 error Failed at the @olympusat-devops/aws-development-account--olympusat-development-dev--us-east-1@1.1.8 TestBase:diff script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

环境

 - **CDK CLI Version :**  1.117.0 (build 0047c98)
 - **Module Version :**  1.117.0
 - **Node.js Version :**  v14.17.4
 - **OS               :**  Linux Zeus 5.11.0-25-generic #27~20.04.1-Ubuntu SMP Tue Jul 13 17:41:23 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
 - **Language (Version):** Typescript (Version 3.9.10)

我什至尝试全局安装 typescript 和 cdk。这个问题仍然存在。

不胜感激。我花了一整天的时间试图解决这个问题,但没有成功。

项目布局:

├── accounts
│   ├── olympusat-development-dev
│   ├── README.md
│   └── us-east-1/
│       ├── cdk.context.json
│       ├── cdk.json
│       ├── cdk.out
│       │   └── ...
│       ├── jest.config.js
│       ├── node_modules
│       │   └── @olympusat-devops
│       │       ├── core -> ../../../../../packages/core
│       │       ├── oly-bastion -> ../../../../../packages/oly-bastion
│       │       ├── oly-efs -> ../../../../../packages/oly-efs
│       │       ├── oly-pipeline -> ../../../../../packages/oly-pipeline
│       │       ├── oly-pipeline-default-image -> ../../../../../packages/oly-pipeline-default-image
│       │       ├── oly-pipeline-templates -> ../../../../../packages/oly-pipeline-templates
│       │       ├── oly-rds-wordpress -> ../../../../../packages/oly-rds-wordpress
│       │       ├── oly-s3-replication-destination -> ../../../../../packages/oly-s3-replication-destination
│       │       ├── oly-s3-replication-source -> ../../../../../packages/oly-s3-replication-source
│       │       ├── oly-vpc -> ../../../../../packages/oly-vpc
│       │       └── oly-web-bucket -> ../../../../../packages/oly-web-bucket
│       ├── package.json
│       ├── README.md
│       ├── src
│       │   ├── index.ts
│       │   └── stacks
│       │       └── ...
│       ├── tests
│       │   └── ...
│       ├── tsconfig.dev.json
│       └── tsconfig.json
├── lerna.json
├── node_modules
│   ├── ...
├── package.json
├── package-lock.json
├── packages
│   ├── core
│   ├── oly-bastion
│   ├── oly-cloudfront-templates
│   ├── oly-efs
│   ├── oly-pipeline
│   ├── oly-pipeline-default-image
│   ├── oly-pipeline-templates
│   ├── oly-rds-wordpress
│   ├── oly-s3-replication-destination
│   ├── oly-s3-replication-source
│   ├── oly-vpc
│   ├── oly-web-bucket
│   └── README.md
├── README.md
└── tsconfig.json

好的,我能够让它工作,但我不确定为什么在当前系统已经工作了一年多之后我必须这样做。我什至让其他开发人员毫无问题地使用它。很奇怪。

为了让它正常工作,我必须做一个简单的改变。

我将cdk.json修改为:

{
  "app": "../../../node_modules/.bin/ts-node --prefer-ts-exts src/index.ts",
  "context": {
    "@aws-cdk/core:enableStackNameDuplicates": "true",
    "aws-cdk:enableDiffNoFail": "true"
  }
}

现在我可以使用 cdk 命令。

这可能是因为我没有在全局安装任何东西。我要求所有包都在本地安装,因为如果您只指定位置,二进制文件也可以这样做。我认为这之前是有效的,因为我最初是在全球范围内安装的,它们可能是它能正常工作的原因。为什么它适用于其他开发人员,我不确定,因为他们没有在全球范围内安装(或者他们说的)。

我的 boostrap 命令的一部分是删除所有 node_modules 目录。问题是它只使用包 node_modules 目录,而不是 Lerna 应该使用的父目录。无论如何,通过指定路径 ../../../node_modules/.bin/ts-node 我确信它现在正在使用正确的 node_modules (父)。

我遇到过类似的问题,但我的构建无法正常工作。

然而,我将 ts-node 安装为全局似乎解决了问题

npm install -g typescript
npm install -g ts-node
npm install -g aws-cdk