Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CDK -- Cannot find module '@aws-cdk/aws-ec2'

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

Am I missing something? Any help would be greatly appreciated and also, thanks in advance! 😄

like image 652
blue2609 Avatar asked Oct 01 '19 04:10

blue2609


2 Answers

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
like image 117
Juned Ahsan Avatar answered Oct 13 '22 11:10

Juned Ahsan


npm install (for install lib)
npm run build (for compile your code)

After that, you can run:

cdk synth
cdk deploy
like image 30
Jian Chen Avatar answered Oct 13 '22 12:10

Jian Chen