Cannot find module '@angular/core' error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
5 min

banner

# Table of Contents

  1. Cannot find module '@angular/core' error
  2. Module not found: Can't resolve '@angular/cdk/scrolling'

# Cannot find module '@angular/core' error

To solve the error "Cannot find module '@angular/core'", make sure you have installed all dependencies by running the npm install command, set the baseUrl option to src in your tsconfig.json file and restart your IDE and development server.

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
# with NPM npm install # or with YARN yarn install

run npm install command

The command installs all of your dependencies.

# Set the baseUrl option to src in your tsconfig.json file

If the error is not resolved, open your main tsconfig file and set the baseUrl option to src. (This could be tsconfig.json or tsconfig.app.json).

tsconfig.json
{ "compilerOptions": { "baseUrl": "src", // ... rest } }

The baseUrl option lets us specify a base directory to resolve non-absolute module names.

With baseUrl set to src, TypeScript will look for files starting at the src folder.

Make sure to restart your IDE and dev server if the error persists.

# Make sure your packages are installed

If the error persists, open your terminal in your project's root directory (where your package.json file is) and rerun npm install.

shell
# with NPM npm install # or with YARN yarn install

The error often occurs when pulling remote code from GitHub and forgetting to install the required packages.

# Delete your node_modules and reinstall your dependencies

If you still get the error, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

# Restart your code editor and development server

Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.

# Setting moduleResolution to node

If the error persists, open your tsconfig.json file and set the moduleResolution property to node.

tsconfig.json
{ "compilerOptions": { "moduleResolution": "node", // ... rest } }

The moduleResolution property should be set to node as this is recommended for most projects.

If you have issues with imports and exports in a TypeScript project, try setting moduleResolution to node.

# Update the versions of your NPM packages

If you still get the error, try to update the versions of your NPM packages by running the following command:

shell
npm update

update versions of npm packages

If you get an error when running the command, try to issue the command with the --legacy-peer-deps option.

shell
npm update --legacy-peer-deps

npm update with legacy peer deps

# Open your code editor in the root directory of your project

If you use VSCode, make sure to open your code editor in the root directory of your project (where the package.json file is located).

If you open your IDE in a different directory, e.g. one directory up, the "Cannot find module '@angular/core'" error occurs.

# Module not found: Can't resolve '@angular/cdk/scrolling'

To solve the error "Module not found: Can't resolve '@angular/cdk/scrolling'", make sure to install the package by opening your terminal in your project's root directory and running the following command: npm i @angular/cdk and restart your IDE and development server.

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
npm install @angular/cdk

install angular cdk

This will add the @angular/cdk package to the dependencies of your project.

If the error is not resolved, try restarting your IDE and your development server.

If you still get the error, try to update the versions of your NPM packages by running the following command:

shell
npm update

# Reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.

# Verify the @angular/cdk package is installed

If the error persists, open your package.json file and make sure it contains the @angular/cdk package in the dependencies object.

package.json
{ // ... rest "dependencies": { "@angular/cdk": "^13.3.1", } }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the package:

shell
npm install @angular/cdk@latest

The @angular/cdk module should NOT be globally installed or be in your project's devDependencies, it should be in the dependencies object in your package.json file.

If you use VS Code, make sure to open your code editor in the root directory of your project (where the package.json file is located).

If you open your IDE in a different directory, e.g. one directory up, the error is caused.

# 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.

Copyright ยฉ 2024 Borislav Hadzhiev