javascript 角度 4 到角度 5 找不到模块“@angular/router”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48451312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular 4 to angular 5 Cannot find module '@angular/router'
提问by Melchia
- I am upgrading from angular 2 to angular 5.
- after upgradation I am seeing the below errors in the the ts file.
- in angular5 do I need to remove @angular/core and @angular/router
- after upgradation providing the package.json also
- providing my error and code snippet below.
- can you tell me how to fix it.
- 我正在从角度 2 升级到角度 5。
- 升级后,我在 ts 文件中看到以下错误。
- 在 angular5 中,我是否需要删除 @angular/core 和 @angular/router
- 升级后还提供 package.json
- 在下面提供我的错误和代码片段。
- 你能告诉我怎么修吗?
error
错误
[ts] Cannot find module '@angular/core'.
[ts] Cannot find module '@angular/router'.
[ts] 找不到模块“@angular/core”。
[ts] 找不到模块“@angular/router”。
code
代码
import {Component, ElementRef, Input,Output,EventEmitter,Inject, OnInit,ViewChild} from '@angular/core';
import {KendoGridComponent} from '../grid/grid.component'
import { Router} from '@angular/router';
package.json
包.json
{
"name": "SPORTS",
"version": "1.0.0",
"description": "SPORTS player - v0.2.37.1",
"engines": {
"node": "7.10.1"
},
"license": "ISC",
"dependencies": {
"@angular/animations": "^5.2.2",
"@angular/common": "^5.2.2",
"@angular/compiler": "^5.2.2",
"@angular/compiler-cli": "^5.2.2",
"@angular/core": "^5.2.2",
"@angular/forms": "^5.2.2",
"@angular/http": "^5.2.2",
"@angular/platform-browser": "^5.2.2",
"@angular/platform-browser-dynamic": "^5.2.2",
"@angular/platform-server": "^5.2.2",
"@angular/router": "^5.2.2",
"@angular/upgrade": "2.0.0",
"@ng-idle/core": "2.0.0-beta.2",
"@ng-idle/keepalive": "2.0.0-beta.2",
"@types/jquery": "^3.2.10",
"@types/node": "^8.0.18",
"@types/underscore": "^1.8.1",
"@types/webpack": "^3.0.5",
"angular2-in-memory-web-api": "0.0.21",
"angular2-router-loader": "^0.3.5",
"angular2-template-loader": "^0.6.2",
"awesome-typescript-loader": "^3.2.1",
"babel-minify-webpack-plugin": "0.2.0",
"bootstrap": "^3.3.6",
"commander": "^2.6.0",
"compression-webpack-plugin": "1.0.1",
"core-js": "^2.4.1",
"css-loader": "^0.28.7",
"del": "^3.0.0",
"express": "^4.14.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.11.2",
"file-saver": "^1.3.3",
"html-webpack-plugin": "^2.30.1",
"http-post": "^0.1.1",
"http-proxy": "^1.8.1",
"jquery": "2.2.3",
"pako": "^1.0.5",
"path": "^0.12.7",
"raw-loader": "^0.5.1",
"reflect-metadata": "^0.1.3",
"require": "^2.4.20",
"rxjs": "^5.5.6",
"style-loader": "^0.18.2",
"text-encoding-shim": "^1.0.0",
"to-string-loader": "^1.1.5",
"ts": "0.0.0",
"typescript": "^2.4.2",
"uglifyjs-webpack-plugin": "^0.4.6",
"underscore": "^1.8.3",
"webpack": "^3.5.6",
"webpack-dev-server": "^2.7.1",
"webpack-merge": "^4.1.0",
"zone.js": "^0.6.23"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-clean-css": "^3.8.0",
"gulp-concat": "^2.6.1",
"gulp-util": "^3.0.8",
"jasmine-core": "~2.4.1",
"jasmine-expect": "^3.0.1",
"jasmine-jquery": "^2.1.1",
"karma": "^1.2.0",
"karma-chrome-launcher": "^0.2.3",
"karma-cli": "^0.1.2",
"karma-coverage": "^1.1.1",
"karma-htmlfile-reporter": "^0.2.2",
"karma-jasmine": "^0.3.8",
"karma-jquery": "^0.1.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-typescript": "^2.1.4",
"karma-typescript-preprocessor": "^0.3.0"
},
"main": "server.js",
"scripts": {
"dev": "webpack-dev-server --config ./webpack.dev.js --inline --progress --color ",
"postinstall": "webpack -p --config ./webpack.prod.js --progress --profile --bail",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://github.iSPORTS.com/player-Replatform/webcomponent.git"
}
}
回答by Melchia
You need to install these dependencies manually.
您需要手动安装这些依赖项。
$ npm i @angular/core -s
$ npm i @angular/router -s
I also faced the same issue while upgrading because of npm. I suggest you start using yarn instead.
由于npm. 我建议你开始使用纱线代替。
回答by joh04667
Is this error coming from the compiler or an IDE tool like Intellisense? I find sometimes with installing/branch switching IDE tools like this can get confused and require a restart of the IDE.
If it won't compile, are the modules actually in your
node_modules?If not, delete yourpackage-lock.jsonorshrinkwrap.jsonand install again. NPM adds the package lock by default as of 5.0 and I find with complicated dependencies it can mess up sometimes.If they are there, run
npm pruneandnpm installagain.If that doesn't work, nuke
node_modulesalongside any lock file and install again.Do any third-party libraries require an update? Angular 5 had some breaking API changes. Check their docs if that's the case.
Consider Yarn.
这个错误是来自编译器还是像 Intellisense 这样的 IDE 工具?我发现有时像这样的安装/分支切换 IDE 工具可能会混淆并需要重新启动 IDE。
如果它不会编译,模块是否真的在你的
node_modules?如果不是,删除你的package-lock.jsonorshrinkwrap.json并重新安装。从 5.0 开始,NPM 默认添加了包锁,我发现复杂的依赖项有时会搞砸。如果他们在那里,运行
npm prune和npm install试。如果这不起作用,请
node_modules与任何锁定文件一起使用核弹并重新安装。是否有任何第三方库需要更新?Angular 5 有一些破坏性的 API 更改。如果是这种情况,请检查他们的文档。
考虑纱线。
I'm pretty sure it's the first one, but I'll leave all the steps I usually take just for the sake of completion.
我很确定这是第一个,但为了完成,我会留下我通常采取的所有步骤。
回答by Jai
Make sure you are exporting RouterModulein each router module that has children. In the angular documentation this is not explicitly stated as being necessary -- it's merely included in the examples -- but it is necessary, as one finds out after the customary several hours of edit-run-sigh, edit-run-shout, edit-run-google-editWithHope-scream-considerSmashingKeyboard, edit-run-cry, edit-run-solveByAccident-feelMomentaryPride (repeat).
确保在每个具有子节点的路由器模块中导出RouterModule。在 angular 文档中,没有明确说明这是必要的——它只是包含在示例中——但这是必要的,正如人们在习惯的几个小时的编辑-运行-叹息、编辑-运行-呼喊、编辑之后发现的那样-run-google-editWithHope-scream-thinkSmashingKeyboard、edit-run-cry、edit-run-solveByAccident-feelMomentaryPride(重复)。
The upshot: include RouterModule. The meta upshot: pay attention to the little seemingly unnecessary code in official documentation.
结果:包括RouterModule。元结果:注意官方文档中看似不必要的小代码。

