1

I'm switching from relative path import to absolute path import for my CDK package, and get this error when run cdk synth:

$ cdk synth
Error: Cannot find module 'lib/CodePipelineStack'

I followed this using absolute paths in typescript for imports to setup the baseUrl and paths in tsconfig.json file.

Not sure why it is not working.

More context

My project structure looks like this:

enter image description here

My tsconfig.json is:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "bin/*": [ "./bin/*" ],
      "lib/*": [ "./lib/*" ],
      "test/*": [ "./test/*" ]
    },
    "target": "ES2018",
    "module": "commonjs",
    "lib": [
      "es2018"
    ],
    "declaration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "cdk.out"
  ]
}

I tried "baseUrl": "./" and "baseUrl": ".", both are not working.

Yang Liu
  • 541
  • 9
  • 26

2 Answers2

5

cdk synth uses ts-node under the hood. ts-node requires an additional package (tsconfig-paths) and some additional configuration to load modules according to your paths config in tsconfig.json:

{
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}

Further documentation: https://github.com/TypeStrong/ts-node#paths-and-baseurl

Your final tsconfig.json would look like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "bin/*": [ "./bin/*" ],
      "lib/*": [ "./lib/*" ],
      "test/*": [ "./test/*" ]
    },
    "target": "ES2018",
    "module": "commonjs",
    "lib": [
      "es2018"
    ],
    "declaration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "cdk.out"
  ], 
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}
sklnd
  • 4,471
  • 3
  • 21
  • 8
0

For windows / does not work. You could use either "sh ./.." or use backslash \