aws / aws/aws-cdk

docs: document how to configure CDK to support `tsconfig.json` alias paths

Open
#16,406 4 comments 5 reactions 1 assignee Claimed by @ryparker View on GitHub
documentation feature-request p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

It would be nice to have docs that describe how we can support tsconfig path aliases.

Currently users must import using relative path:
```ts
import AWS_ACCOUNT_ID from '../../../constants';
```

Instead i'd like to use aliases:
```ts
import AWS_ACCOUNT_ID from '@constants';
```

This is a feature available via the [`paths` option in Typescript's `tsconfig.json`.
](https://www.typescriptlang.org/tsconfig#paths)

**UPDATE**: See zbarbuto's [comment](https://github.com/aws/aws-cdk/issues/16406#issuecomment-1243161969) describing how to get this working.

### Use Case

This is useful when infrastructure is organized across nested directories that sometimes reference each other.

### Proposed Solution

Update the docs with include steps on how to support this feature.

### Other

Currently if you configure the `paths` option in `tsconfig.json` and use an alias and run `tsc` then Typescript successfully transpiles the code however running `cdk synth` throws a module not found error:

```sh
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module '@constants'
Require stack:
- /Users/ryanparker/Documents/GitHub/infrastructure-cdk/build/js/stacks/usa-counts/index.js
- /Users/ryanparker/Documents/GitHub/infrastructure-cdk/build/js/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/Users/ryanparker/Documents/GitHub/infrastructure-cdk/build/js/stacks/usa-counts/index.js:14:22)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/ryanparker/Documents/GitHub/infrastructure-cdk/build/js/stacks/usa-counts/index.js',
'/Users/ryanparker/Documents/GitHub/infrastructure-cdk/build/js/index.js'
]
}
Subprocess exited with error 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
```

* [x] :wave: I may be able to implement this feature request
* [ ] :warning: This feature might incur a breaking change

---

This is a :rocket: Feature Request

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.