Issues with Node ESM, TypeScript and AWS CDK

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

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.


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.

Full-stack developer. In love with Typescript and Serverless

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store