TS2307 Build:Cannot 查找模块 'lodash'

TS2307 Build:Cannot find module 'lodash'

更新

抱歉,我没有把我的问题解释清楚。我得到的错误不是在通过 Gulp 编译 Angular 应用程序时,而是在 Visual Studio 'Errors List' 中。从那以后,我已经能够解决这个问题(至少是解决方法),但我要感谢所有做出回应的人。我的决议如下。


我在 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 package

npm install --save @types/lodash

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

此外,typings 不再是推荐的方法。您应该卸载并删除 typings,并且您应该仅通过将 tsconfig 更新为:

来切换到 @types 软件包
"typeRoots": [
  "node_modules/@types"
],

最后,删除类型后,您需要为 core-jsjasmine 添加 @types 包以启用这些包:

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

再次感谢所有回复的人,对于最初没有更清楚地了解实际问题,我深表歉意。

我的 TypeScript 文件编译正确,但 Visual Studio 继续在 'Error List' window 中报告构建错误。

在尝试了一些步骤(上面列出的步骤)后,我在这里找到了解决问题的方法: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 根据需要更新文件。现在重新构建您的应用程序。