【问题标题】:How to import javascript node module in typescript?如何在打字稿中导入javascript节点模块?
【发布时间】:2017-02-21 20:19:35
【问题描述】:

我正在尝试导入 xml2js 模块,但遇到 404 错误说 xml2js 未找到

import xml2js from 'xml2js';

如何在 TypeScript 中导入 node_modules 目录中的 JavaScript 模块?

【问题讨论】:

标签: javascript typescript npm npm-install


【解决方案1】:

我认为您需要安装或至少编写一个模块声明。以下是确定类型的声明文件:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/xml2js/xml2js.d.ts

您可以使用类型来安装这些模块声明。 https://github.com/typings/typings

关于声明文件: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

您也可以编写自己的模块声明,但我建议先查看注册表。如果没有声明文件,TypeScript 将无法理解任何模块。

希望有所帮助;)

【讨论】:

    【解决方案2】:

    需要添加参考路径

    /// <reference path="node_modules/xml2jse/xml2js.d.ts" />
    import xml2js = require("xml2js");
    

    记住你需要一个 xml2js 的 typescript 模块

    tsd install xml2js
    

    【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2021-12-25
    • 2021-08-20
    • 1970-01-01
    • 2016-06-14
    • 2015-03-07
    • 1970-01-01
    • 2013-02-25
    • 2018-06-10
    相关资源
    最近更新 更多