Skip to content

(init): remove bin field from TypeScript app template's package.json #1202

@tmokmss

Description

@tmokmss

Describe the feature

The TypeScript template generated by cdk init app --language typescript includes a bin field in package.json:

{
  "name": "my-app",
  "bin": {
    "my-app": "bin/my-app.js"
  }
}

This field should be removed from the template as it does not seem to serves any purpose for CDK apps and can cause issues.

Use Case

The bin field declares a CLI command for npm packages intended to be installed globally or via npx. A CDK app is neither. It is invoked by the CDK CLI through the app field in cdk.json (e.g. npx ts-node bin/my-app.ts).

The bin field also references bin/my-app.js, which does not exist at init time. Modern templates use ts-node/tsx to run .ts directly, so compiling to .js is not part of the expected workflow.

When the project directory is named cdk, the template generates "bin": { "cdk": "bin/cdk.js" }. If the user then runs tsc (e.g. for type-checking in CI), bin/cdk.js is emitted. At that point, npx cdk synth can resolve to this project's bin/cdk.js instead of the AWS CDK CLI via the npx cache. The CDK app is executed directly as if it were the CLI — it is not a CLI, so the behavior is completely broken.

We hit this issue in practice: aws-samples/remote-swe-agents#377

Proposed Solution

Remove the bin field from the generated package.json in the TypeScript init template. It has no effect on CDK app functionality.

Other Information

The root cause chain when the directory is named cdk:

  1. cdk init generates "bin": { "cdk": "bin/cdk.js" } from the directory name
  2. tsc emits bin/cdk.js (no outDir or noEmit in the default tsconfig)
  3. npx cdk synth resolves to this project's bin/cdk.js via the npx cache instead of aws-cdk CLI
  4. The CDK app is executed directly as a CLI command — this is completely wrong and results in arbitrary errors

Workarounds:

  • Add "noEmit": true to tsconfig.json
  • Rename the bin key to something other than cdk
  • Delete the bin field from package.json after cdk init

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS CDK Library version (aws-cdk-lib)

2.187.0

AWS CDK CLI version

2.1030.0

Environment details (OS name and version, etc.)

Linux (Ubuntu 24.04 on WSL2), Node.js v24.13.1, npm 11.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions