AWS CDK

Issues with Node ESM, TypeScript and AWS CDK

Michael Timbs
1 min readFeb 23, 2021

--

This is a very short, un-paywalled survivorship guide to using the AWS CDK with TypeScript and node ESM.

TL;DR: ts-node fails when there are ES modules in the dependency graph (not specific to CDK but you need it to run cdk synth).

If you are using ESM you should already have "type": "module" in your package.json

If you’ve used the CLI to add the CDK to a TypeScript project you will get a generated file cdk.ts that looks something like this

If you run cdk synth you are likely to get the following error

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" ......OR maybe something like thisnode_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:374
throw new ERR_MODULE_NOT_FOUND(

The easiest way to resolve this issue is to install this zero dependency package and then update the script in cdk.json to the following

"app": "node --loader @k-foss/ts-esnode --experimental-specifier-resolution=node ./bin/cdk.ts",

You should now be able to run cdk synth with no issues.

Thoughts

So far Node + ESM has been an absolute nightmare of debugging and dependency hell. I’d hesitate to jump in until things get a bit more mature.

--

--

Michael Timbs

Full-stack developer. In love with Typescript and Serverless