【问题标题】:Typescript/Javascript can't find moduleTypescript/Javascript 找不到模块
【发布时间】:2016-08-30 11:30:27
【问题描述】:

我正在尝试安装一个名为“request”https://github.com/request/request 的模块

在我的 angular 2 typescript 项目中,但由于某种原因我无法将其导入。

我尝试使用 npm install --save request 以正常方式安装,并且我也尝试了 typings install request --ambient --save 虽然我不知道它到底是做什么的。

我正在处理这个样板文件https://github.com/mgechev/angular2-seed,它在 wiki 中建议安装模块就像使用 npm install 一样简单,然后

 import * as jwt from 'angular2-jwt/angular2-jwt';

但由于某种原因,我无法导入我的请求模块。

这是我的导入行

import * as request from 'request';

我是否需要以某种方式在其他地方引用该模块?

【问题讨论】:

  • 你做了 npm install 吗?
  • typings 用于定义文件。您是否尝试使用npm install request 安装它?
  • 是的,我安装了 npm。我没有提到,因为我认为这是显而易见的

标签: typescript angular


【解决方案1】:

request 模块并非专为 Node 应用程序的浏览器而设计。您应该改用browser-request

话虽如此,使用 NPM 安装模块并不能直接用于您的应用程序:

  • 用于编译您需要安装一个用于编译的打字机。你的库的 API 的一种契约。这样,TypeScript 编译就会知道模块中存在哪些类、方法和属性。

  • 为了执行,您需要在加载应用程序时引用您的模块。例如,对于 SystemJS,您需要这样的东西:

    System.config({
      map: {
        request: 'node_modules/browser-request/index.js'
      }
    });
    

    这样您就可以通过这种方式导入库:

    import * as request from 'request';
    

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 2016-09-20
    • 2016-10-24
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多