17

I just started playing around with AWS CDK yesterday and I found something very weird.

First of all, I'm using TypeScript for my CDK app (I used cdk init --language typescript to generate the project files and I tried to import aws-ec2 module so this is what I did:

import cdk = require('@aws-cdk/core');
import ec2 = require('@aws-cdk/aws-ec2');

export class vpcStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {

//.... all other codes go here....

However, when importing the aws-ec2 module this way, I got this error when trying to deploy the stack:

⨯ Unable to compile TypeScript:
lib/cdk-type_script-stack.ts:2:22 - error TS2307: Cannot find module '@aws-cdk/aws-ec2'.

2 import ec2 = require('@aws-cdk/aws-ec2');
                       ~~~~~~~~~~~~~~~~~~

Subprocess exited with error 1

This is very weird because the API docs right here clearly stated that this is how I should import the aws-ec2 module in TypeScript

blue2609
  • 841
  • 2
  • 10
  • 25

5 Answers5

20

You need to install the node package before you could import and use it

Execute below on the command line to install npm package for aws-cdk

npm i @aws-cdk/aws-ec2
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • ohhhhh okay, right right. Huh..., okay so this is very interesting. It looks like if I specified **cdk init --language javascript", it has all the node modules available for us but if I specified typescript, we have to do it manually, is that correct? Thanks for such quick reply btw :) – blue2609 Oct 01 '19 at 04:34
  • On my laptop, 'cdk init --language javascript' doesn't install any modules except core. I'm using 1.10.0. – piljoong Oct 01 '19 at 10:17
  • @piljoong Hmmm that's weird alright..., `cdk init --language javascript` installed all the required modules from the get go for me but that's not the case with TypeScript. Anyway, it's all good, the issue has been resolved so thanks heaps guys! :) – blue2609 Oct 01 '19 at 20:36
5
npm install (for install lib)
npm run build (for compile your code)

After that, you can run:

cdk synth
cdk deploy
Jian Chen
  • 51
  • 2
1

You may have a version of npm that is incompatible with the version of @aws-cdk/pipelines as explained here: https://github.com/aws/aws-cdk/issues/13541#issuecomment-801606777

Sawyer Merchant
  • 1,243
  • 2
  • 12
  • 21
0

In addition to @juned-ashan 's answer, verify that you are installing the correct module version that corresponds to your cdk version (and other cdk modules installed).

For example:

$ npm install --save @aws-cdk/aws-ec2@1.10.0

Note: not enough points to add this as a comment in Juned's answer.

Cchor
  • 21
  • 6
0

This simply means that your aws-ec2 module is not found and you have to install the required module using below command

npm i @aws-cdk/aws-ec2

I would suggest using CDK version 2 as v1 is outdated and support for version 1 is also ended in June 2023

user12256545
  • 2,755
  • 4
  • 14
  • 28