aws-cdk(Typescript) error :Argument of type 'xx' is not assignable to parameter of type 'IConstruct'. Types of property 'node' are incompatible.
はじめに
そこのあなた! もしかして、aws-cdkで型はあっているのに下のエラーメッセージが出てきてお困りではありませんか?
Type 'import("/<ディレクトリまでのパス>/node_modules/@aws-cdk/aws-iam/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode' is not assignable to type 'import("/<ディレクトリまでのパス>/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode'. Types have separate declarations of a private property 'host'.
この記事では、原因の確認方法とその解決策を紹介します。
環境
$ cdk --version 1.88.0 (build f65009b)
原因
cdk関連でインストールしたモジュールの全て
のバージョンが一致していないと発生するようです。
確認
方法1:
先ほどの原因の部分でもお伝えしたように、まずはじめにpackage.jsonの @aws-cdk/xxx
のバージョンが全て同じか確認しましょう
例えば、以下のようにバージョンが一つでも違う場合は発生する可能性があります
"dependencies": { "@aws-cdk/assert": "1.88.0", "@aws-cdk/aws-ec2": "1.85.0", "@aws-cdk/aws-elasticloadbalancingv2": "1.82.0", "@aws-cdk/aws-elasticloadbalancingv2-targets": "1.79.0",
方法2:
package.jsonでバージョンを揃えたのに解消されない・・?
次はdependenciesごとに設定したバージョンになっているか確認してみましょう
- 作業しているディレクトリに
cd
- 次に以下のコードを入力する。複数のバージョンがインストールされているか確認する
$ cat node_modules/\@aws-cdk/*/package.json | grep version | sort | uniq "version": "1.88.0", "version": "1.89.0",
もしバージョンの違うdependenciesを知りたい場合、以下のコマンドを入力する
$ cat node_modules/\@aws-cdk/*/package.json | grep -B1 "\"version\": \"1.89.0\"" "name": "@aws-cdk/cloud-assembly-schema", "version": "1.89.0", -- "name": "@aws-cdk/core", "version": "1.89.0", -- "name": "@aws-cdk/cx-api", "version": "1.89.0", -- "name": "@aws-cdk/region-info", "version": "1.89.0",
解決方法
node_modules/\@aws-cdk
とpackage-lock.json
を削除npm install
それではやってみましょう
$ rm package-lock.json $ rm -r node_modules/\@aws-cdk/ $ npm install added 32 packages, and audited 660 packages in 17s $ cat node_modules/\@aws-cdk/*/package.json | grep version | sort | uniq "version": "1.88.0", $ cdk synth Resources: vpcA2121C38: Type: AWS::EC2::VPC
まとめ
cdkのバージョンアップのサイクルが早いので、このエラーに遭遇する確率は高いです。
なので、この記事でエラーに躓く人が一人でも減ったらと思います