angular - TS2307 内部版本 :Cannot find module 'lodash'

标签 angular typescript visual-studio-2015 npm-install

更新

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


我在 PoC Asp.Net Core Web 应用程序中构建了一个 Angular4/TypeScript 应用程序。 Angular4 应用程序在我的 PoC 应用程序中运行良好,因此我将所有相关文件复制到我正在开发的应用程序(也是一个 Asp.Net Core 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"
  ]
}

最佳答案

你应该安装@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

关于angular - TS2307 内部版本 :Cannot find module 'lodash' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43526413/

相关文章:

visual-studio-2015 - VS 2015 许可证已过期

javascript - 在新选项卡中打开 PDF 文件 angular 4?

angular - 在延迟加载的模块被销毁时采取行动

javascript - 使用解析的日期获取后,Typescript 将 JSON 转换为对象

json - 为什么 Visual Studio 2015 在编辑 .json 文件时会锁定?

c# - 访问路径被拒绝 (Xamarin/Android)

Angular2,HostListener,我如何定位一个元素?我可以根据类(class)定位吗?

angular - 将对象插入数组并 *ngFor 它

typescript - Angular 2 beta.17 : Property 'map' does not exist on type 'Observable<Response>'

css - 将背景应用于元素 :after dynamically with ngStyle