1

I have a TypeScript CDK Project. Per the recommendation I read somewhere, I've put both my infrastructure and runtime code into the same project.

The runtime code is required to use an obscure js library which does not have any type definitions. I've created a custom .d.ts file with types as described here. The TypeScript compiler is happy with this and tsc builds correctly.

When however I run cdk synth I get the "Could not find a declaration file for module 'xyz'" error. I'm guessing that the CDK can't find the definition file, however it's in the same location as other .ts files.

I've edited cdk.json and removed "**/*.d.ts" from the exclude filter to no effect.

tsconfig.json:

    {
      "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "lib": [
          "es2020"
        ],
        "declaration": true,
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": false,
        "inlineSources": true,
        "experimentalDecorators": true,
        "strictPropertyInitialization": false,
        "typeRoots": [
          "./types",
          "./node_modules/@types"
        ],
        "outDir": "./out",                                   
        "esModuleInterop": true,                             
        "forceConsistentCasingInFileNames": true,            
        "sourceMap": true,                                
      },
      "include":[
        "lib",
        "bin",
        "lib/**/*",
        "bin/**/*",
        "types"
      ],
      "exclude": [
        "node_modules",
        "cdk.out",
        "./out/**/*",
        "test/**/*.spec.ts"
      ]
    }

cdk.json:

    {
      "app": "npx ts-node --prefer-ts-exts bin/LeopardGeotabApp.ts",
      "watch": {
        "include": [
          "**",
          "lib/geotab/mg-api.js.d.ts"
        ],
        "exclude": [
          "README.md",
          "cdk*.json",
          "**/*.d.ts",
          "**/*.js",
          "tsconfig.json",
          "package*.json",
          "yarn.lock",
          "node_modules",
          "test"
        ]
      },
      "build": "",
      "versionReporting": true,
      "context": {
        "@aws-cdk/aws-lambda:recognizeLayerVersion": true,
        "@aws-cdk/core:checkSecretUsage": true,
        "@aws-cdk/core:target-partitions": [
          "aws",
          "aws-cn"
        ],
        "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
        "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
        "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
        "@aws-cdk/aws-iam:minimizePolicies": true,
        "@aws-cdk/core:validateSnapshotRemovalPolicy": true,
        "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
        "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
        "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
        "@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
        "@aws-cdk/core:enablePartitionLiterals": true,
        "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
        "@aws-cdk/aws-iam:standardizedServicePrincipals": true,
        "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true
      }
    }

I'm just executing cdk synth with no parameters.

fedonev
  • 20,327
  • 2
  • 25
  • 34
Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56
  • 1
    How are you compiling the Typescript? Does your `cdk.json` include a `build` command? – fedonev Jan 06 '23 at 13:00
  • Hi. I wasn't specifically compiling the TypeScript on ```cdk synth``` as I assumed that the CDK was doing it (as my CDK code is written in TypeScript). There was no build step in my cdk.json file. After your suggestion I added "build": "npx tsc" into it. It hasn't affected the issue though. – Damien Sawyer Jan 07 '23 at 15:03
  • I made a little progress. If I create the .d.ts file under node_modules/@types then the CDK sees it. Eg \node_modules\@types\mg-api-js\mg-api-js.d.ts. This isn't really workable though for team sharing reasons. When I create a folder /types and put that file in, then include /types in tsconfig.json --> typeRoots, it fails again. I'm wondering if CDK has its own tsconfig somewhere it's using. I haven't been able to find anything. – Damien Sawyer Jan 07 '23 at 15:51
  • 1
    The CDK CLI is written in TS, but is app-language agnostic. It performs no TS/JS build actions on your code beyond the ones you have given it in the `cdk synth --app` command and `cdk.json`, as far as I know. Do you have a standard project structure? What is your `tsconfig.json` and `cdk.json` and `cdk synth` command? – fedonev Jan 07 '23 at 16:43
  • I understand that you can develop for the CDK with multiple languages. Having said that, I'm writing my CDK infrastructure code in TypeScript. I presumed that at some point it would be compiling it. I've updated the question with my config files. I was thinking, I could probably put my .d.ts file in a folder which is excluded from TypeScript compilation (but still outside of node_modules for ease of editing) then modify 'package.json' --> 'scripts/build' to copy that file into /node_modules/@types before compiling. Seems a bit hacky though, and you'd also have to build before synthing. – Damien Sawyer Jan 08 '23 at 00:30
  • For all languages, the CDK CLI does what you tell it (`--build`, then `--app`). Does `npx ts-node --prefer-ts-exts bin/LeopardGeotabApp.ts` run without error on its own? How about `node ./path/to/LeopardGeotabApp.js`? – fedonev Jan 08 '23 at 09:33
  • Thanks @fedonev, that did the trick. Those didn't run on their own, but I hooked in to the app setting to temporarliy copy the d.ts file into node_modules/@types. The synth now works. A little hacky... but does the job. – Damien Sawyer Jan 08 '23 at 23:48

1 Answers1

0

I got around this by modifying cdk.json

{
  "app": "cp ./types/mg-api-js.d.ts ./node_modules/@types/ && npx ts-node --prefer-ts-exts bin/LeopardGeotabApp.ts && rm ./node_modules/@types/mg-api-js.d.ts",
....
}

If I didn't delete the file after the ts-node step, then if I ran tsc on its own it would fail because of the duplication of the module in the .d.ts file.

Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56