【问题标题】:Angular 2 cannot find module after npm installAngular 2 在 npm install 后找不到模块
【发布时间】:2016-12-04 10:13:06
【问题描述】:

我有一个 Angular 2 应用程序,我正在尝试按照软件包自述文件中的说明在其中安装 angular2-autosize 组件 (https://github.com/stevepapa/angular2-autosize)。我在下面跑:

npm install angular2-autosize --save

成功完成。我可以在“node_modules”目录中看到一个angular2-autosize 文件夹。
此外,我的 package.json 中添加了一个依赖项 "angular2-autosize": "^1.0.1"

现在,我转到我的 app.module.ts 文件,并添加以下内容:

import { Autosize } from 'angular2-autosize';
@NgModule({
    declarations: [
    ..
    Autosize

并尝试使用ng build 进行构建,但失败了,

找不到模块“angular2-autosize”。

【问题讨论】:

    标签: angular npm dependencies


    【解决方案1】:

    我变了

    import { Autosize } from 'angular2-autosize';

    app.module.ts 文件中,
    到,

    import { Autosize } from 'angular2-autosize/angular2-autosize';

    它成功了:)

    【讨论】:

    • 出现“tsconfig.json中不包含对应文件”错误怎么办?
    【解决方案2】:

    这个 npm 包有很多问题(实际版本是 1.0.1)。 实际上这个包没有 index.ts 文件,所以你需要引用它包含的文件。为此,您需要在导入中指定文件:

    import { Autosize } from 'angular2-autosize/angular2-autosize';
    

    其次,该包不包含捆绑 javascript 文件,因此当您编译应用程序时,编译器会希望在您自己的 javacsript 文件中包含 Autosize 指令。你应该在你的 javscript 文件中看到这样的东西(我知道它很丑):

    System.register("../../node_modules/angular2-autosize/src/autosize.directive", ["@angular/core"], function (exports_67, context_67) {
    ...
    });
    

    但可能是在您的 tsconfig.json 文件中您排除了 node_modules 目录,因此在这种情况下您可能会收到如下错误:

    tsconfig.json 中不包含相应文件

    在这种情况下,您可以尝试从 tsconfig.json 文件的排除条目中删除 node_modules 目录。

    【讨论】:

      猜你喜欢
      • 2015-02-26
      • 2015-12-16
      • 1970-01-01
      • 2016-02-25
      • 2022-10-23
      • 2020-03-25
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多