2017-04-20 640 views
3

更新TS2307构建:无法找到模块“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应用越来越尽管具有这让我觉得这更多的是一个环境问题的项目中不需要做任何改变同样的错误。

我已通读Similar Issue他们说要安装@ 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" 
 
    ] 
 
}

回答

0

再次感谢所有回复的人,我对最初没有对实际问题做出更明确的道歉表示歉意。

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

尝试了一些步骤是固定的问题,我发现这里的东西(上述任何更多的上市)后:http://rostacik.net/2015/08/14/how-to-disable-building-of-typescript-files-in-visual-studio-2015/

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

1

您应该安装@types/lodashpackage通过:

npm install --save @types/lodash 

这将告诉打字稿编译什么lodash是什么,模块和组件是由库曝光。

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

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

最后,一旦取下分型,你将需要添加@typescore-jsjasmine,使那些包:

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

不要忘了在你的tsconfig.json文件中''types“:[”node“,”lodash“]''lodash''''types”:[“node”]'' – mjwrazor

+0

@ mjwrazor你确定在Typescript 2.0+中这是必需的吗?我的理解是,这不应该是必要的:https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html –

+0

我约95%确定你需要。我在我的第二个Typescript包中,目前需要使用它。出于某种原因,我的同事似乎不需要将它用于所有包。上面他安装了节点并使用它。在经历了许多令人头痛之后,我现在默认为默 – mjwrazor

相关问题