4
votes

So I'm a cdk and typescipt beginner. After successfully deploying a couple of stacks I'm not getting the following error with cdk synth: Unexpected token export. Subprocess exited with error 1.

I'm less interested in solving this issue and more interested to where the stack trace is, or any kind of additional info about the error. Doing a --trace or -v does not really provide much helpful info.

Any ideas how I can obtain such information????

3

3 Answers

1
votes

I believe the issue is because npx is used to run ts-node under the hood and npx appears to swallow the stack as described here.

One work around is add a try/catch block, e.g.

try {
  main()
} catch (e) {
  console.log(e)
  throw e
}
0
votes

What happened behind the scene is that CDK converts to stack into a cloudformation template and saves it into S3 - The S3 bucket created when running cdk bootstrap(More info here).

When you run cdk synth, CDK trying to convert the code ( in your case typescript) into cloudformation stack. this error: Unexpected token export. could be since async call that didn't end, in addition, this error means that your code could not be transferred into cloudformation stack, but it doesn't mean your "cdk" code is broke.

When you run cdk deploy cdk compare the transferred template with the S3 template. And deploy only the diffs.

Update:

Yesterday DevopsStart publish new article about debuuging cdk in vs code. This might be helpful.

CDK Debugging in VSCode.

0
votes

So I think this is being caused by some javascript (yours or possibly in an imported module) that is using ESM export syntax.

This confused me a little at first because I'm using import/export syntax all over my project however the project is written in typescript which means that it's being compiled to javascript before execution and most likely all of these ESM statements are being emitted in CJS syntax. I however was also using lodash-es because I preferred the import syntax. This module internally uses ESM syntax in javascript and as such this is interpreted directly by the node runtime. Modern versions of node can use ESM syntax however only under specific package configs which mine did not have. My solution was just to remove the offending package however you may be able to go the other direction and configure your package.json such that ESM is enabled in node. This may also require your tsconfig.json to be updated such that the typescript compiler also emits ESM.

https://nodejs.org/api/esm.html