繁体   English   中英

TS2307 Build:找不到模块'lodash'

[英]TS2307 Build:Cannot find module 'lodash'

更新

我道歉,我没有很清楚地解释我的问题。 我得到的错误不是通过Gulp编译Angular应用程序,而是在Visual Studio的“错误列表”中。 我已经能够解决这个问题(至少是解决方法),但我要感谢所有回复的人。 我的决议如下。


我在PoC Asp.Net核心Web应用程序中构建了一个Angular4 / TypeScript应用程序。 Angular4应用程序在我的PoC应用程序中运行完美,然后我将所有相关文件复制到我正在开发的应用程序(也是Asp.Net核心Web应用程序)。

然后我运行'npm install'以确保安装了所有依赖项并使用Gulp在我的wwwroot / js /文件夹中创建.js文件。 Gulp完成没有错误但现在Visual Studio报告下面的错误并阻止我构建项目。

TS2307 Build:Cannot find module 'lodash'.

更令人困惑的是,在完成上述步骤后,尽管没有对项目进行任何更改,原始的PoC应用程序也会出现相同的错误,这让我觉得这更像是一个环境问题。

我已阅读类似问题 ,他们说要安装@ types / lodash,但这会导致重复错误(卸载打字会修复重复错误,但会导致其他错误)。

抛出错误的.ts代码是:

import * as _ from "lodash";

改为:

import _ from "lodash";

不起作用。

提前感谢你的任何帮助,因为我正在试图解决为什么这不起作用。

我的配置文件如下:

的package.json

 { "version": "1.0.0", "name": "aspnet", "private": true, "scripts": { "postinstall": "typings install", "typings": "typings" }, "dependencies": { "@angular/common": "~4.0.0", "@angular/compiler": "~4.0.0", "@angular/core": "~4.0.0", "@angular/forms": "~4.0.0", "@angular/http": "~4.0.0", "@angular/platform-browser": "~4.0.0", "@angular/platform-browser-dynamic": "~4.0.0", "@angular/router": "~4.0.0", "angular-in-memory-web-api": "~0.3.0", "angular2-datatable": "^0.6.0", "bootstrap": "^3.3.7", "core-js": "^2.4.1", "lodash": "^4.17.4", "moment": "^2.14.1", "reflect-metadata": "^0.1.3", "rxjs": "5.0.1", "systemjs": "0.19.40", "zone.js": "^0.8.4" }, "devDependencies": { "@types/node": "7.0.7", "gulp": "^3.9.1", "gulp-clean": "^0.3.2", "gulp-concat": "^2.6.1", "gulp-less": "^3.1.0", "gulp-sourcemaps": "^1.6.0", "gulp-tsc": "^1.3.1", "gulp-typescript": "^3.1.6", "gulp-uglify": "^2.0.0", "path": "^0.12.7", "typescript": "^2.1.0", "typings": "^2.1.0" } } 

typings.json

  { "globalDependencies": { "core-js": "registry:dt/core-js#0.0.0+20160725163759", "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", "node": "registry:dt/node#6.0.0+20160909174046" } } 

tsconfig.json

  { "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true, "module": "commonjs", "noEmitOnError": true, "noImplicitAny": false, "outDir": "../wwwroot/app/", "removeComments": false, "sourceMap": true, "target": "es5", "moduleResolution": "node", "typeRoots": [ "./node_modules/@types", "./node_modules" ], "types": [ "node" ] }, "exclude": [ "node_modules" ] } 

您应该通过以下方式安装@types/lodash

npm install --save @types/lodash

这将告诉Typescript编译器lodash是什么以及库公开了哪些模块和组件。

此外, typings不再是推荐的方法。 您应该卸载并删除typings ,你应该切换到@types只能通过更新您的tsconfig到包:

"typeRoots": [
  "node_modules/@types"
],

最后,一旦你删除了打字,你需要为core-jsjasmine添加@types包以启用这些包:

npm install --save @types/core-js
npm install --save @types/jasmine

再次感谢所有回复的人,并为最初对实际问题不清楚而道歉。

我的TypeScript文件正在正确编译,但Visual Studio继续在“错误列表”窗口中报告构建错误。

在尝试了许多步骤(上面列出的那些步骤之后)之后,我找到了解决问题的方法: http//rostacik.net/2015/08/14/how-to-disable-building-of-打字稿-文件-在视觉工作室2015 /

简而言之,使用文本编辑器打开项目“.xproj”文件(可能是“.csproj”)。 找到<PropertyGroup> </PropertyGroup>标记并添加行<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked> 保存文件,然后允许Visual Studio根据需要更新文件。 现在重新构建您的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM