hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 442 forks source link

Support ECMAScript modules (ESM) in CDKTF projects #1660

Open bestickley opened 2 years ago

bestickley commented 2 years ago

Community Note

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

ansgarm commented 2 years ago

Hi @bestickley 👋 Why do you need to change the package type to module?

bestickley commented 2 years ago

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.

ansgarm commented 2 years ago

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?

bestickley commented 2 years ago

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.

cyrfer commented 8 months ago

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"
jason-henriksen commented 8 months ago

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.

cyrfer commented 3 months ago

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 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?