gpt4 book ai didi

typescript - AWS CodeBuild tsc 错误 TS2307 : Cannot find module

转载 作者:行者123 更新时间:2023-12-04 04:31:09 26 4
gpt4 key购买 nike

我创建了几个不同的 TypeScript cdk 管道,它们在 CodeBuild 阶段都遇到了相同的 tsc 错误。
其中两个管道是通过 cdk 文档复制的:

  • https://docs.aws.amazon.com/cdk/latest/guide/codepipeline_example.html
  • https://cdkworkshop.com/20-typescript/70-advanced-topics/200-pipelines.html

  • 本质上, npm i (或 npm ci )+ tsc 在本地工作正常,但是当通过 CodeBuild 完成时,似乎我的依赖项没有安装它们的依赖项,导致 tsc 中断。
    有没有其他人遇到过这个问题?或者换句话说,为什么 npm install + tsc 在 CodeBuild 上表现不同?
    代码构建日志 1:
    [Container] 2021/03/11 19:11:35 Waiting for DOWNLOAD_SOURCE
    [Container] 2021/03/11 19:11:36 Phase is DOWNLOAD_SOURCE
    [Container] 2021/03/11 19:11:36 CODEBUILD_SRC_DIR=/codebuild/output/src038984068/src
    [Container] 2021/03/11 19:11:36 YAML location is /codebuild/readonly/buildspec.yml
    [Container] 2021/03/11 19:11:36 Processing environment variables
    [Container] 2021/03/11 19:11:36 No runtime version selected in buildspec.
    [Container] 2021/03/11 19:11:36 Moving to directory /codebuild/output/src038984068/src
    [Container] 2021/03/11 19:11:36 Registering with agent
    [Container] 2021/03/11 19:11:36 Phases found in YAML: 2
    [Container] 2021/03/11 19:11:36 PRE_BUILD: 1 commands
    [Container] 2021/03/11 19:11:36 BUILD: 2 commands
    [Container] 2021/03/11 19:11:36 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
    [Container] 2021/03/11 19:11:36 Phase context status code: Message:
    [Container] 2021/03/11 19:11:36 Entering phase INSTALL
    [Container] 2021/03/11 19:11:36 Phase complete: INSTALL State: SUCCEEDED
    [Container] 2021/03/11 19:11:36 Phase context status code: Message:
    [Container] 2021/03/11 19:11:36 Entering phase PRE_BUILD
    [Container] 2021/03/11 19:11:36 Running command npm ci
    added 772 packages in 7.728s

    [Container] 2021/03/11 19:11:48 Phase complete: PRE_BUILD State: SUCCEEDED
    [Container] 2021/03/11 19:11:48 Phase context status code: Message:
    [Container] 2021/03/11 19:11:48 Entering phase BUILD
    [Container] 2021/03/11 19:11:48 Running command npm run build

    > cdk-s3-sns-lambda@0.1.0 build /codebuild/output/src038984068/src
    > tsc

    node_modules/@aws-cdk/aws-codebuild/lib/pipeline-project.d.ts(1,27): error TS2307: Cannot find module 'constructs' or its corresponding type declarations.
    (...cascade of missing module errors)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 2
    npm ERR! cdk-s3-sns-lambda@0.1.0 build: `tsc`
    npm ERR! Exit status 2
    代码构建日志 2:
    [Container] 2021/03/11 19:54:10 Waiting for agent ping
    [Container] 2021/03/11 19:54:15 Waiting for DOWNLOAD_SOURCE
    [Container] 2021/03/11 19:54:16 Phase is DOWNLOAD_SOURCE
    [Container] 2021/03/11 19:54:16 CODEBUILD_SRC_DIR=/codebuild/output/src363431369/src
    [Container] 2021/03/11 19:54:16 YAML location is /codebuild/readonly/buildspec.yml
    [Container] 2021/03/11 19:54:16 Processing environment variables
    [Container] 2021/03/11 19:54:16 No runtime version selected in buildspec.
    [Container] 2021/03/11 19:54:16 Moving to directory /codebuild/output/src363431369/src
    [Container] 2021/03/11 19:54:16 Registering with agent
    [Container] 2021/03/11 19:54:16 Phases found in YAML: 2
    [Container] 2021/03/11 19:54:16 INSTALL: 1 commands
    [Container] 2021/03/11 19:54:16 BUILD: 2 commands
    [Container] 2021/03/11 19:54:16 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED
    [Container] 2021/03/11 19:54:16 Phase context status code: Message:
    [Container] 2021/03/11 19:54:16 Entering phase INSTALL
    [Container] 2021/03/11 19:54:16 Running command npm install
    npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. Ill try to do my best with it!
    npm WARN pipeline@0.1.0 No repository field.
    npm WARN pipeline@0.1.0 No license field.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

    added 762 packages from 491 contributors and audited 764 packages in 12.459s

    28 packages are looking for funding
    run `npm fund` for details

    found 0 vulnerabilities


    [Container] 2021/03/11 19:54:30 Phase complete: INSTALL State: SUCCEEDED
    [Container] 2021/03/11 19:54:30 Phase context status code: Message:
    [Container] 2021/03/11 19:54:30 Entering phase PRE_BUILD
    [Container] 2021/03/11 19:54:30 Phase complete: PRE_BUILD State: SUCCEEDED
    [Container] 2021/03/11 19:54:30 Phase context status code: Message:
    [Container] 2021/03/11 19:54:30 Entering phase BUILD
    [Container] 2021/03/11 19:54:30 Running command npm run build

    > pipeline@0.1.0 build /codebuild/output/src363431369/src
    > tsc

    node_modules/@aws-cdk/aws-codebuild/lib/artifacts.d.ts(1,21): error TS2307: Cannot find module '@aws-cdk/aws-s3' or its corresponding type declarations.
    (...more missing module errors)
    npm ERR! code ELIFECYCLE
    npm ERR! errno 2
    npm ERR! pipeline@0.1.0 build: `tsc`
    npm ERR! Exit status 2
    其他详情:
  • 我确保我所有的@aws-cdk/* deps 都安装在完全相同的版本
  • 在 cdk 1.60.0、1.92.0、1.93.0
  • 上重现问题
  • 来自 CodeBuild 日志 1 的 PipelineStack 类:
  • // lib/pipeline-stack.ts
    import * as cdk from '@aws-cdk/core';
    import * as codecommit from '@aws-cdk/aws-codecommit';
    import * as codepipeline from '@aws-cdk/aws-codepipeline';
    import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions';
    import { SimpleSynthAction, CdkPipeline } from '@aws-cdk/pipelines';

    export class PipelineStack extends cdk.Stack {
    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id);

    const repo = new codecommit.Repository(this, 'UploadRepo', {
    repositoryName: 'cdk-s3-sns-lambda'
    });

    const sourceArtifact = new codepipeline.Artifact();
    const cloudAssemblyArtifact = new codepipeline.Artifact();

    new CdkPipeline(this, 'CodePipeline', {
    crossAccountKeys: false,
    cloudAssemblyArtifact,
    sourceAction: new codepipeline_actions.CodeCommitSourceAction({
    actionName: 'CodeCommit',
    output: sourceArtifact,
    repository: repo
    }),
    synthAction: SimpleSynthAction.standardNpmSynth({
    sourceArtifact,
    cloudAssemblyArtifact,
    buildCommand: 'npm run build'
    }),
    });
    }
    }


    // bin/pipeline.ts
    #!/usr/bin/env node
    import 'source-map-support/register';
    import * as cdk from '@aws-cdk/core';
    import { PipelineStack } from '../lib/pipeline-stack';

    const app = new cdk.App();
    new PipelineStack(app, 'AppPipelineStack');

  • 从 CodeBuild 日志 1 生成 buildspec.yaml:
  • {
    "version": "0.2",
    "phases": {
    "pre_build": {
    "commands": [
    "npm ci"
    ]
    },
    "build": {
    "commands": [
    "npm run build",
    "npx cdk synth"
    ]
    }
    },
    "artifacts": {
    "base-directory": "cdk.out",
    "files": "**/*"
    }
    }

    最佳答案

    我一直面临同样的问题。
    此 GH-Issue ( https://github.com/aws/aws-cdk/issues/13339 ) 中的解决方法帮助了我。
    我像这样更改了 CDK 中的构建规范

    synthAction: SimpleSynthAction.standardNpmSynth({
    sourceArtifact,
    cloudAssemblyArtifact,
    installCommand: 'npm i -g npm && npm ci'
    }),
    这将为流水线构建步骤生成以下(内联)构建规范:
    {
    "version": "0.2",
    "phases": {
    "pre_build": {
    "commands": [
    "npm i -g npm && npm ci"
    ]
    },
    "build": {
    "commands": [
    "npx cdk synth"
    ]
    }
    },
    "artifacts": {
    "base-directory": "cdk.out",
    "files": "**/*"
    }
    }
    在第一次推送代码并运行管道后,我不得不自己修改构建规范,因为管道突变步骤是在构建步骤之后触发的。因此 ne buildspec 设置将在变异步骤之后应用。

    关于typescript - AWS CodeBuild tsc 错误 TS2307 : Cannot find module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66590492/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com