Fix CDK - Argument of type Not Assignable to type Construct

avatar

Borislav Hadzhiev

Last updated: Apr 14, 2022

banner

Check out my new book

Solving Argument of type is not Assignable to Construct in CDK #

The reason we get the "Argument of type is not assignable to parameter of type Construct" error in CDK is because we have different versions of CDK packages in the same CDK project.

Different versions of CDK packages are often incompatible with each other, for example:

package.json
{ "dependencies": { "@aws-cdk/aws-dynamodb": "^1.100.0", "@aws-cdk/aws-iam": "^1.101.0", } }

In the code snippet, the versions of the packages differ and this causes the error.

In order to solve the "Argument of type is not assignable to parameter of type Construct" error in CDK, we have to:

  1. Update our CDK packages in the dependencies section in package.json to the same version.

    Remove the ^ (caret) symbol prefix from the versions.

    Npm adds the ^ symbol by default. The ^ symbol tells npm to install the version we have specified or install the latest minor or patch version.

    When using CDK packages, we want to lock the version down, therefore we remove the ^ prefix.

  1. Delete the node_modules folder and the package-lock.json file:
shell
rm -rf node_modules rm package-lock.json
  1. install the dependencies
shell
npm install

The next time you install a CDK package, use the --save-exact flag, so that you lock down the version of the package and tell npm to omit the ^ symbol prefix.

To install a specific version of a CDK package and lock it:

shell
npm install --save-exact @aws-cdk/aws-s3@1.101.0

To install the latest version of a CDK package and lock it:

shell
npm install --save-exact @aws-cdk/aws-s3@latest

If you look at the dependencies section in your package.json file, after installing a module with the --save-exact flag, you will see that the ^ (caret) symbol is not included and the version is locked down.

package.json
{ "dependencies": { "@aws-cdk/aws-s3": "1.101.0" } }
Make sure to check your CDK package versions in the package.json file, before installing new CDK packages. The CDK library gets updated almost daily, so unfortunately it's not as simple as installing the latest version, when adding new dependencies.

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.