Fix CDK - Argument of type Not Assignable to type Construct


Borislav Hadzhiev

Fri Apr 30 20212 min read


Photo by Joshua Earle

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

The reason we get the "Argument of type is not assignable to paramter 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:

{ "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:
rm -rf node_modules rm package-lock.json
  1. install the dependencies
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:

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

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

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.

{ "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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee