Last updated: Apr 4, 2024
Reading timeยท5 min
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:
# with NPM npm install # or with YARN yarn install
The command installs all of your dependencies.
baseUrl
option to src
in your tsconfig.json
fileIf 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
).
{ "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.
If the error persists, open your terminal in your project's root directory
(where your package.json
file is) and rerun npm install
.
# 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.
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
.
# 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.
# 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.
moduleResolution
to node
If the error persists, open your tsconfig.json
file and set the
moduleResolution
property to node
.
{ "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
.
If you still get the error, try to update the versions of your NPM packages by running the following command:
npm update
If you get an error when running the command, try to issue the command with the
--legacy-peer-deps
option.
npm update --legacy-peer-deps
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.
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:
npm install @angular/cdk
@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:
npm update
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
.
# 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.
# 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
If the error persists, open your package.json
file and make sure it contains
the @angular/cdk
package in the dependencies
object.
{ // ... rest "dependencies": { "@angular/cdk": "^13.3.1", } }
You can try to manually add the line and re-run npm install
.
npm install
Or install the latest version of the package:
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.
package.json
file is located).If you open your IDE in a different directory, e.g. one directory up, the error is caused.
You can learn more about the related topics by checking out the following tutorials: