Solving Cannot find Module Error in AWS CDK

avatar
Borislav Hadzhiev

Last updated: Jan 26, 2024
2 min

banner

# Cannot find module 'aws-cdk/*' Error in AWS CDK

There are two common reasons why we usually get the "Cannot find module" Error in CDK:

  • We forgot to install a module we've imported.
  • We have the necessary packages installed but they are glitched and we have to delete the node_modules directory and run npm install again.

I was working on a project yesterday where I had all of the necessary dependencies but I kept getting the error one package name after another:

cannot find module

To solve the error, we have to:

  1. Check if we have installed all of the CDK packages we're importing. You can run the npm ls command to list all packages, or just go through your package.json file.
  2. If we have all CDK packages installed, remove the node_modules directory and run the install command again:
shell
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
  1. If we're missing a package, we have to install it, for example:
shell
npm install --save-exact @aws-cdk/aws-dynamodb@latest

To test whether the issue is resolved - run the npx aws-cdk synth command.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.