2

In shell scripts interacting with the AWS CDK Toolkit, I often see both npx -- cdk and npx -- aws-cdk. Apparently, there are two npm packages cdk and aws-cdk. What is the difference between these two?

Moreover, I make confusing observations when interacting with these two packages.

npx -- cdk --version     # output: Need to install the following packages: cdk@2.32.1
npx -- aws-cdk --version # output: Need to install the following packages: aws-cdk@2.32.1

Since neither package is installed locally, both commands want to install the corresponding package, fine.

npm install -g cdk@2.32.0
npx -- cdk --version     # output: 2.32.0 (build 00e0c2d)
npx -- aws-cdk --version # output: Need to install the following packages: aws-cdk@2.32.1

After installing cdk locally, npx -- cdk executes the local package and npx -- aws-cdk still wants to install aws-cdk, also fine.

npm install -g aws-cdk@2.32.0
npx -- cdk --version     # output: 2.32.0 (build 00e0c2d)
npx -- aws-cdk --version # output: Need to install the following packages: aws-cdk@2.32.1

Now it gets confusing. Although we locally installed aws-cdk rather than cdk, npx -- cdk still executes some local package and npx -- aws-cdk still wants to install aws-cdk. Why?

user1494080
  • 2,064
  • 2
  • 17
  • 36

1 Answers1

1

The aws-cdk package provides a cdk command, the entry point to the AWS Cloud Development Kit.

When you install aws-cdk package in node_modules/.bin there is a cdk command installed. This allows one to use cdk command inside package.json scripts.

The cdk command is also the only command added to .bin by aws-cdk.

As per all of the documentation, in order to interact with AWS CDK from CLI, one should use cdk command. That is even though the package name is aws-cdk.

Now, the npx provides a convenient way to run a command from a package regardless if it is already installed.

This works in the most straightforward way when the package name matches the command name it provides. This is not the case for aws-cdk. In order to fix that, and allow anyone to run npx cdk ... the AWS CDK decided to publish a package named cdk. This is a small wrapper around cdk command provided by aws-cdk package.

miensol
  • 39,733
  • 7
  • 116
  • 112
  • To me it seems that `aws-cdk` is the preferred way, and not `cdk`. At least the documentation always uses `npx aws-cdk` and not `npx cdk`, e.g. see [here](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-typescript.html), [here](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-javascript.html) or [here](https://docs.aws.amazon.com/cdk/v2/guide/cli.html). – user1494080 Jul 17 '22 at 11:12
  • Yes. What meant is that without using npx the documentation will use cdk in CLI. It's only in the context of npx where aws-cdk is used in CLI. I presume that because of that the cdk package was published just in case. – miensol Jul 17 '22 at 20:09