How To Solve The Error: “Cannot Find Module ‘@Angular/core'” In JavaScript

This guide can help you learn how to solve the error “Cannot find module ‘@angular/core'” in JavaScript. Let’s follow this article to learn more about it with the explanation and examples below.

How does the error “Cannot find module ‘@angular/core'” happen?

The error “Cannot find module ‘@angular/core’” in JavaScript occurs when you have not installed all dependencies in the terminal, or you have not set the ‘baseUrl’ option as ‘src’ in your json file. Another reason is that you have installed angular core as a global. Intellisense can’t find it in node_modules. When you get one or more of these reasons, you will get an error like this.

cannot find module '@angular/core'.
cannot find module '@angular/router'.
Cannot find module 'rxjs/Observable'.
Cannot find module 'rxjs/Subject'.
…

After learning about how this error occurs, follow the next title to learn how to fix it.

How to solve this error?

To solve the error “Cannot find module ‘@angular/core'” you can install all dependencies in the terminal or you can set the ‘baseUrl’ option as ‘src’ in your json file. These are some solutions that can help you fix this error.

Install all dependencies in the terminal

A simple way to solve this error is to install all dependencies by the following command in the terminal of your project.

npm install 

This way will work in most cases.

If it does not work with your case, follow the next solution. It will help you solve the problem.

Set the ‘baseUrl’ option as ‘src’ in the json file

Another solution is setting the ‘baseUrl’ option as ‘src’ in your json file. You simply add the line “baseUrl”: “src” in your json file. The error will be fixed.

Look at the example below to learn more about this solution.

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": "src", // Add this line.
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

Summary

These are some solutions that can help you solve the error “Cannot find module ‘@angular/core’” in JavaScript. Choose the solution that is the best for you. We hope this tutorial is helpful to you and you can solve your problem after reading this article. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *