VSCode cannot find module '@angular/core' or any other modules
Asked Answered
G

38

156

My project was generated with Angular CLI version 1.2.6.

I can compile the project and it works fine, but I always get error in VSCode telling me:

cannot  find module '@angular/core'
cannot find module '@angular/router'
cannot find module .....

screenshot screenshot

I have attached content of my tsconfig.json file this has been really frustrating for me, spending 2 hours to figure out what is wrong, I have also uninstalled and reinstalled the VSCode it doesn't work.

Here is my environment specification:

@angular/cli: 1.2.6
node: 6.9.1
os: win32 x64
@angular/animations: 4.3.4
@angular/common: 4.3.4
@angular/compiler: 4.3.4
@angular/core: 4.3.4
@angular/forms: 4.3.4
@angular/http: 4.3.4
@angular/platform-browser: 4.3.4
@angular/platform-browser-dynamic: 4.3.4
@angular/router: 4.3.4
@angular/cli: 1.2.6
@angular/compiler-cli: 4.3.4
@angular/language-service: 4.3.4

OS: Microsoft vs 10 enterprise

project root folder

.angular-cli.json
.editorconfig
.gitignore
.vscode
e2e
karma.conf.js
node_modules
package.json
protractor.conf.js
README.md
src
tsconfig.json
tslint.json

node_modules folder

-@angular
--animations
--cli
--common
--compiler
--compiler-cli
--core
---@angular
---bundles
---core.d.ts
---core.metadata.json
---package.json
---public_api.d.ts
---README.md
---src
---testing
---testing.d.ts
---testing.metadata.json
--forms
--http
--language-service
--platform-browser
--platform-browser-dynamic
--router
--tsc-wrapped
@ng-bootstrap
@ngtools
-@types
--jasmine
--jasminewd2
--node
--q
--selenium-webdriver

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}
Gauntry answered 27/8, 2017 at 9:25 Comment(8)
It's because you've installed angular core as global. Intellisense cannot find it in node_modules.Lebna
angular core is in my node_moduleGauntry
Did you open a project in vscode before you npm install? If yes have you try to restart vscode after that ?Hartmann
i have restarted 100 timesGauntry
Can you try to generate a new project using cli, npm install and then see if there is the same thing with that one?Hartmann
for me, I had to reinstall angularfire2Mischance
LOL Thanks! Restarting VSCode sorted it for me. This was driving me mad!Davit
Restarting VSCode worked for me as well. Why doesn't Intellisense kind of reload after a npm install ? Sounds like a basic feature to me.Zarla
G
1

I uninstalled all extension I had already installed, and it turns out JavaScript and TypeScript IntelliSense extension from below address caused the issue. https://marketplace.visualstudio.com/items?itemName=sourcegraph.javascript-typescript

the point here is when you visit the website you see there is a yellow label, telling you it is in preview release, but when you browse in vs extensions, you don't see that label.

Gauntry answered 29/8, 2017 at 4:16 Comment(3)
Unfortunately I'm still having this issue, even after uninstalling all extensions and restarting VS Code. :/Mischance
Apologies, I should have given an update on this. Yes, it seems to have cleared up, but I'm not sure I recall exactly how. However, some things I definitely did do: A) completely deleted and re-created my solution, B) uninstalled and re-installed Angular CLI, C) rebooted my machine. Hope this might help someone. If it happens again, I'll try to be more methodical about determining how I fix it, so that I can report back a repeatable solution.Mischance
Another possible solution is to click on the status bar and select "Use Workspace Version", see github.com/Microsoft/vscode/issues/34681Disequilibrium
X
188

I was facing this issue only while importing my own created components/services For those of you like me, for whom the accepted solution did not work, can try this:

Add

"baseUrl": "src"

in your tsconfig.json. The reason is that visual code IDE is unable to resolve the base url so is unable to resolve path to imported components and gives error/warning.

Whereas angular compiler takes src as baseurl by default so it is able to compile.

NOTE:

You need to restart VS Code IDE to make this change come into effect.

EDIT:

As mentioned in one of the comments, in some cases changing workspace version might also work. More details here: https://github.com/Microsoft/vscode/issues/34681#issuecomment-331306869

Xavier answered 19/2, 2018 at 10:3 Comment(10)
this worked for me...but now getting another error Build: Class 'Subject<T>' incorrectly extends base class 'Observable<T>'Overby
@Overby that does not seem to be related to this issue. Maybe post it as a new question alongwith your code.Xavier
This worked for me. I added src to basUrl in the tsconfig.app.json file. "baseUrl": "src",Setzer
Adding "baseUrl":"src" in the tsconfig.json file worked for me.Clairvoyant
Nice by Adding "baseUrl": "src" this in tsconfig and restart IDE resolve my issue.Thirtythree
So I'm here in 2019 and I'm still getting this error. I tried adding the baseURL above but this didn't help (did restart editor). I also tried to disable workspace > enable workspace after going to a TS file > right click version number > manage extension.Felipe
So I did this and the url was already there. So instead of doing it as explained above, I just restarted vs code and it fixed the issue.Makalu
note : to restart VScode Open the command palette ( Ctrl + Shift + P ) and execute the command: >Reload Window.Racon
being a newbie looking for this answer, it may be useful to share that this needs to be added under the "compilerOptions" in the tsconfig.json as mentionedGreeson
I has something similar. I named my module MyFile and not MyFile.ts.Fogg
P
129

Most likely missing node_modules package in the angular project, run:

npm install

inside the angular project folder.

Preheat answered 14/7, 2018 at 17:18 Comment(4)
The directory tree in the original question contained the node_moduls/@angular/core folder, but maybe it was empty.Ralfston
then restart VSCodeDuong
Or, in my case (Windows file manager) MOVE the blame thing (node_modules folder) BACK to ClientApp where it belongs, where you had moved it from earlier.Misogyny
@Duong Thank you! Just solved my issue ! VSCode needs to be restartedSailor
S
65

Visual Code restart is needed if any update or install or clear cache

Sidky answered 10/8, 2018 at 10:0 Comment(1)
VS Code updated without me knowing about it. Restart helped.Gross
L
29

the fix for me was to run

npm install

and then unload,then reload the project in visual studio.

Leaving answered 29/5, 2018 at 22:51 Comment(0)
A
19

I was facing this issue in my angular 5 application today. And the fix which helped me, was simple. I added "moduleResolution": "node" to the compilerOptions in the tsconfig.json file. My complete tsconfig.json file content is below.

{
  "compileOnSave": false,  
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

The moduleResolution specify module resolution strategy. The value of this settings can be node or classic. You may read more about this here.

Anatollo answered 7/7, 2018 at 16:27 Comment(3)
saved my career!Roxane
Best answer ever!Speiss
To the top with you!Whitewing
T
11

Delete Node Modules folder from project folder.Run below command

npm cache clean --force npm install

It should work.

Tacho answered 7/5, 2020 at 5:24 Comment(1)
thanks, It works :)Erivan
S
11

I was facing the same issue , there could be two reasons for this-

  1. Your src base folder might not been declared, to resolve this go to tsconfig.json and add the baseUrl as "src"
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
  1. you might have problem in npm , To resolve this , open your command window and run-npm install
Snatchy answered 12/9, 2020 at 15:39 Comment(0)
P
9

In the VSCode status bar, it must be showing typescript version - like this

enter image description here

Clicking on that version number will show you this, different versions available.

enter image description here

If you are using the VSCode version then switching to Workspace version solves the problem if it is VScode issue rather than your tsconfig.json (I am already using that one, so not highlighted)

enter image description here

Prokopyevsk answered 13/9, 2020 at 12:12 Comment(1)
thank you man, I was loosing living motivations ...Quanta
E
6

I had the same problem. I resolved it by clearing npm cache which is at "C:\Users\Administrator\AppData\Roaming\npm-cache"

Or you can simply run:

npm cache clean --force

and then close vscode, and then open your folder again.

Estate answered 24/9, 2018 at 7:36 Comment(0)
C
6

You need to install it manually.

$ npm i @angular/core -s
Chyme answered 8/7, 2020 at 18:52 Comment(0)
S
6

I faced the same problem , As I'm trying to work on angular project in VS code.

The solution for which this issue resolved is .

  1. Delete the node_modules folder if you have one in your project folder

2.run the following command in terminal

npm install

  1. Then Run npm audit fix

  2. Then Run npm audit fix --force

now the issue will be resolved.

Sniffy answered 29/1, 2021 at 9:3 Comment(0)
F
5

Try using:

npm audit fix --force

and then:

npm install --save @ng-bootstrap/ng-bootstrap

instead of saving @ng-bootstrap/ng-bootstrap globally.

Florineflorio answered 6/9, 2018 at 15:32 Comment(0)
H
5

I had the same issue, was strange because project compiled and ran without errors. I updated npm and then reinstalled the packages

npm update
npm install

then vs code stop saying that.

Holly answered 8/1, 2020 at 14:21 Comment(0)
C
5

I've encountered this problem and fixed it by following commands.

npm cache clean --force
npm i @angular/core -s

And then don't forget to restart your VS code. It must be fixed.

Clapp answered 11/8, 2021 at 12:59 Comment(0)
T
2

I tried a lot of stuff the guys informed here, without success. After, I just realized I was using the Deno Support for VSCode extension. I uninstalled it and a restart was required. After restart the problem was solved.

Tortile answered 6/5, 2020 at 23:37 Comment(0)
L
2

Most likely npm package is missing. And sometimes npm install does not fix the problem.

I have faced the same and I have solved this issue by deleting the node_modules folder and then npm install

Leake answered 10/8, 2020 at 9:30 Comment(0)
U
2

This also happens when you are downloading/cloning the code from a source control like GIT. The node modules are not pushed to the source control using .gitignore file and you need to run npm install the first time you get the artifacts from the repo.

Unasked answered 21/9, 2022 at 18:55 Comment(0)
G
1

I uninstalled all extension I had already installed, and it turns out JavaScript and TypeScript IntelliSense extension from below address caused the issue. https://marketplace.visualstudio.com/items?itemName=sourcegraph.javascript-typescript

the point here is when you visit the website you see there is a yellow label, telling you it is in preview release, but when you browse in vs extensions, you don't see that label.

Gauntry answered 29/8, 2017 at 4:16 Comment(3)
Unfortunately I'm still having this issue, even after uninstalling all extensions and restarting VS Code. :/Mischance
Apologies, I should have given an update on this. Yes, it seems to have cleared up, but I'm not sure I recall exactly how. However, some things I definitely did do: A) completely deleted and re-created my solution, B) uninstalled and re-installed Angular CLI, C) rebooted my machine. Hope this might help someone. If it happens again, I'll try to be more methodical about determining how I fix it, so that I can report back a repeatable solution.Mischance
Another possible solution is to click on the status bar and select "Use Workspace Version", see github.com/Microsoft/vscode/issues/34681Disequilibrium
C
1

this extension https://marketplace.visualstudio.com/items?itemName=sourcegraph.javascript-typescript was causing me the error in visual code, I uninstalled it and it works for me

Colophony answered 10/10, 2018 at 23:16 Comment(1)
"This extension is now unpublished from Marketplace. You can choose to uninstall it." as of 11 Nov 2019 mentioned on marketplace.visualstudio.com/…Posehn
F
1

In my case it was a mispelling of the import line. Check that you have spelt the @angular/core part properly if typing it out manually.

import { Component } from '@angular/core';
Felipe answered 23/4, 2019 at 10:49 Comment(0)
D
1

The fix for me was to import the entire project. For those who have this problem in 2019 please check if you have imported the entire project not a part of the project.

Dirtcheap answered 3/7, 2019 at 11:41 Comment(0)
C
1

If you did what I (foolishly) did... Which was drag and drop a component folder into my project then you'll probably be able to solve it by doing what I did to fix it.

Explanation: Basically, by some means Angualar CLI must tell InteliJ what @angular means. If you just plop the file in your project without using the Angular CLI i.e. ng g componentName --module=app.module then Angular CLI doesn't know to update this reference so IntelliJ has no idea what it is.

Approach: Trigger the Angular CLI to update references of @angular. I currently know only one way to do this...

Implementation: Add a new component at the same level as the component your having issues with. ng g tempComponent --module=app.module This should force the Angular CLI to run and update these references in the project. Now just delete the tempComponent you just created and don't forget to remove any reference to it in app.module.

Hope this helps someone else out.

Cohort answered 30/10, 2019 at 20:33 Comment(0)
E
1

In my case, when i upgrade vs project to angular 10, I had this errors.

Angular cli creates tsconfig.json, tsconfig.base.json and tsconfig.app.json when i delete tsconfig.json and rename tsconfig.base.json to tsconfig.ts all things will Ok. you must also change extends inside tsconfig.app.json to tsconfig.json

Econometrics answered 15/12, 2020 at 12:37 Comment(0)
U
1

I had same problems with Sublime Text.

I came up with following solution: I just edited

tsconfig.json

in the root of Angular workspace to include my freshly created application.

 {
  "files": [],
  "references": [
    {
      "path": "./projects/client/tsconfig.app.json"
    },
    {
      "path": "./projects/client/tsconfig.spec.json"
    },
    {
      "path": "./projects/vehicle-market/tsconfig.app.json"
    },
    {
      "path": "./projects/vehicle-market/tsconfig.spec.json"
    },
    {
      "path": "./projects/mobile-de-lib/tsconfig.lib.json"
    },
    {
      "path": "./projects/mobile-de-lib/tsconfig.spec.json"
    }
  ]
    }
Utricle answered 23/12, 2020 at 13:58 Comment(0)
M
1

For me it was that I ran 'npm install' in one of the projects and thought that was enough, but actually the solution/repo had 2 projects so I also needed to run 'npm install' in the other one too as they had separate package.json files. Reading the docs helped me to understand this;

This command installs a package and any packages that it depends on.

A package is a folder containing a program described by a package.json file.

https://docs.npmjs.com/cli/v7/commands/npm-install

Misinform answered 3/3, 2023 at 15:3 Comment(0)
W
1

Make sure you are in the right directory. if you are inside any pages directory the npm i command doesn't work. Go to the base directory to run the command

Warrick answered 30/8, 2023 at 6:27 Comment(0)
T
0

All you need to do is that you have to include nodes_modules folder in your project. You might face this problem when you clone any project from github through git command line.

Tensiometer answered 23/3, 2018 at 9:40 Comment(3)
probably not the case since when you run npm install on the root of your site in the folder that contains your package.json file, this folder is automatically created for you with the modules needed.Gypsy
yah. if run nmp install then it will download necessary package by checking package.json file..Tensiometer
You should mention that they should run npm install. Otherwise, your answer doesn't help at all.Selfcommand
J
0

Occurs when cloning or opening existing projects in Visual Studio Code. In the integrated terminal run the command npm install

Jugulate answered 25/6, 2018 at 11:6 Comment(1)
This was already given multiple times. Anything new you'd like to add?Jenn
M
0

Executing the following two commands solves the problem for me:

npm install -g @angular/cli
ng update --all --force
Marketable answered 28/4, 2020 at 9:7 Comment(2)
Posting multiple identical answers is, at best, "highly frowned upon" and your answers are liable to be deleted. In your case, you have also misspelled Angular in all of the answers.Dehorn
Please add some explanation to your code such that others can learn from itJenn
P
0

for Visual Studio -->

    Seems like you don't have `node_modules` directory in your project folder.
     
    Execute this command where `package.json` file is located:
    
     npm install 
Paresis answered 21/8, 2020 at 19:14 Comment(0)
S
0

Try this, it worked for me:

npm i --save @angular/platform-server  

Then reopen VS code

Shocking answered 4/10, 2020 at 14:47 Comment(0)
H
0

This worked for me.

 npm install --save-dev @angular-devkit/build-angular
Henrie answered 23/11, 2020 at 20:53 Comment(0)
Q
0

I had the same issue and none of these solutions worked for me.

I'm using Ubuntu in WSL2. It turns out VS Code needed to install the WSL extension to gain full access to folders in the WSL directory. After that, the errors disappeared.

Quartan answered 31/3, 2023 at 15:11 Comment(0)
F
0

You can see this error if your @angular/cli version does not match the version of the @angular packages being referenced in the project. For me, @angular/core in my project was version 13.X.X but @angular/cli was version 16.X.X (because this is a new VM where I freshly installed Angular). I updated my project's packages to the latest versions, and the project successfully built. npm-check-updates is a useful tool for identifying what packages can/should be upgraded.

So my steps were:

  • I had a fresh install of the latest version of Angular.
  • Run npm install -g npm-check-updates to install the npm-check-updates tool
  • Run ncu --upgrade to update my packages.json file w/ the latest package versions
  • Run npm install (from within my project's base directory) to install the latest versions packages. Note - you can add the -- force flag if you don't care that the CLI is warning you about conflicts... this may be necessary when making big sweeping changes.

Another option would be to downgrade your version of @angular/cli to match the version of angular being referenced in your project.

Foretell answered 13/9, 2023 at 22:12 Comment(0)
S
-1

Do run

npm install 

it will work most of the cases

Schwejda answered 8/2, 2019 at 12:8 Comment(2)
This is incorrect. The questions states that it is compiling correctly and the the issue is with VSCode, not the compilationJuju
My project was compiling correctly and running, and the issue was within VSCode, not the compilation, and npm install just fixed it.Quack
G
-2

I solved this problem as follow:

  1. Open Visual studio code with your project.
  2. Terminal -> New Terminal.
  3. Write npm install.
Gilder answered 17/4, 2019 at 11:1 Comment(1)
There are already at least 2 different answers saying "run npm install"Hellenistic
P
-3

From my point of view the CLI you are using and the libraries are mismatched. The ionic CLI version 1 cannot build libraries for ionic CLI version 4. The best solution is to try upgrade your CLI version. You can otherwise use nvm which allows you to run multiple node versions on the same O.S. This can help you use different ionic CLI versions across different projects depending on the requirements.

Check out nvm @: Their official windows repo. There is also a MAC and Linux version.

Pumpkinseed answered 8/8, 2018 at 7:34 Comment(0)
C
-6

If we get this type of error just use the command:

 npm i @anglar/core,
 npm i @angular/common,
 npm i @angular/http,
 npm i @angular/router

After installing this also showing error just remove few words then again add that word its working.

Collenecollet answered 21/6, 2018 at 11:17 Comment(1)
The original package.json had them already installed. What does “remove a few words” mean?Ralfston

© 2022 - 2024 — McMap. All rights reserved.