Search code examples
typescriptaws-cdkaws-codepipelineaws-codebuild

AWS CDK Code Pipeline Development - error TS1005: ']' expected


Problem: I am building a new CodePipeline with the aws-cdk-lib/pipelines library in the CDK, and am facing a typescript error within the code build stage.

Error Message:

Folder structure abbreviated

[Container] 2023/01/03 07:48:21 Running command npm run build

> [email protected] build
> tsc

intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,18): error TS1005: ']' expected.
intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,21): error TS1005: ';' expected.
intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,35): error TS1005: '(' expected.
intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,36): error TS1135: Argument expression expected.
intoMagentoQueueConsumer/node_modules/axios/index.d.ts(6,50): error TS1005: ')' expected.
intoMagentoQueueConsumer/node_modules/axios/index.d.ts(7,1): error TS1128: Declaration or statement expected.

[Container] 2023/01/03 07:48:36 Command did not exit successfully npm run build exit status 2
[Container] 2023/01/03 07:48:36 Phase complete: BUILD State: FAILED
[Container] 2023/01/03 07:48:36 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm run build. Reason: exit status 2

I have included the other error messages of TS1135 and TS1128 for context, as they all might be related.

CDK Code Pipeline Stack:

Note, in my commands, I cd into each Lambda, install it with npm ci, and then cd out. I'm removed this code as it is quite verbose. It was at the top of the commands key value pair. tsc -v results in the latest TypeScript version.

this.codePipeline = new CodePipeline(this, `${environment}-${appName}-`, {
  pipelineName: `${environment}-${appName}-`,
  selfMutation: true,
  crossAccountKeys: false,
  role: this.codePipelineRole,
  synth: new ShellStep("Deployment", {
    input: CodePipelineSource.codeCommit(this.codeRepository, environment, {  codeBuildCloneOutput: true }),
    installCommands: ["npm i -g npm@latest", "npm install -g typescript"],
    commands: [
      "tsc -v", 
      "npm ci",
      "npm run build",
      "cdk synth",
    ],
  })
});

Lambda Function package.json:

{
  "name": "intomagentoqueueconsumer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-sfn": "^3.226.0",
    "axios": "^1.2.1",
    "axios-retry": "^3.3.1"
  }
}

Lambda Function Build in the CDK:

this.intoMagentoQueueConsumer = new NodejsFunction(this, `intoMagentoQueueConsumer`, {
  runtime: Runtime.NODEJS_16_X,
  memorySize: 1024,
  handler: "handler",
  depsLockFilePath: join(__dirname, "../../src/lambda-handlers/queue-consumers/intoMagentoQueueConsumer/package-lock.json"),
  entry: join(__dirname, "../../src/lambda-handlers/queue-consumers/intoMagentoQueueConsumer/index.ts"),
  bundling: { 
    minify: false,
    nodeModules: ["@aws-sdk/client-sfn", "axios", "axios-retry"],
    externalModules: ["aws-sdk", "crypto-js"],
   },
});

EDIT: I've changed the install command for typescript to npm install -g typescript --save-dev and obtained a Version of 4.9.4. This did not, however, fix the issue.


Solution

  • To fix this problem:

    1. Ensure all NodejsFunction code refers to the root package.json file and DOES NOT have it's own package.json
    2. Ensure the latest version of Typescript (mine was 4.9.4), is installed in the development dependencies.