terraform-cdk
terraform-cdk copied to clipboard
Support ECMAScript modules (ESM) in CDKTF projects
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
cdktf & Language Versions
cdktf: 0.9.4 Node.js: 16.14.1
Affected Resource(s)
S3BucketObject
Debug Output
https://gist.github.com/bestickley/1f15626e5770692b45bbc974a709b7c6
Expected Behavior
Success
Actual Behavior
Error
Steps to Reproduce
"type": "module"
in package.json and use S3BucketObject
construct.
Important Factoids
References
- #0000
Hi @bestickley 👋 Why do you need to change the package type to module?
Because from the Node.js docs:
ECMAScript modules are the official standard format to package JavaScript code for reuse
and I'd like to use the official standard format. Not a huge deal, but a nice to have.
Ah, I see! We use ts-node
in our template which might cause this problem. I found this blog post about using ESM with the AWS CDK which seems like a solution to a similar problem.
Nevertheless, I'm going to tag this as issue awaiting-evidence
for now, as we don't plan to proactively work on this but are still open for contributions or revisiting this if things change. I hope that is okay with you?
I thought the issue was specific to the s3 module and how it reads files but I definitely could be wrong. That blog post is talking about issues arising from having ESM packages in your node_modules. My understanding is that the current cdktf s3 library is using Node.js APIs in a way that's not compatible with ESM. Yes, I'm ok with that tag.
I believe this bit me today. All the packages in my monorepo are ESM now.
When I try to run cdktf synth
it fails:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
This is hitting me right now.
I run extensive tests in jest. I haven't found a way to use cdk to run jest. But if I don't have type:'module' just will file. If I do have it, then CDK fails. I need both to play nice.
I would like to be able to package a ESM LIBRARY that leverages cdktf and related providers. This would make it much simpler to be able to depend on other ESM libraries.
I have other packages which consume that library. Currently I am only able to package the library as CJS so that it is consumed as CJS where the cdktf CLI is used. I don't know how to get the cdktf CLI to work when my CJS library containing cdktf code depends on ESM libraries.
UPDATE
I still want to be able to bundle a library as ESM but I am unblocked.
I was able to get the cdktf CLI to work with my CJS library that depends on a ESM library by following the blog post mentioned above by @ansgarm. I edited cdktf.json to use tsx instead of ts-node.
Ah, I see! We use
ts-node
in our template which might cause this problem. I found this blog post about using ESM with the AWS CDK which seems like a solution to a similar problem. Nevertheless, I'm going to tag this as issueawaiting-evidence
for now, as we don't plan to proactively work on this but are still open for contributions or revisiting this if things change. I hope that is okay with you?
If you're using Bun, just replace app
value in cdktf.json
with "bun main.ts"
.
Example:
{
...
"app": "bun main.ts",
...
}