简体   繁体   English

如何在打字稿中导入javascript节点模块?

[英]How to import javascript node module in typescript?

I am trying to import xml2js module , but facing 404 error saying xml2js not found 我正在尝试导入xml2js模块,但xml2js 404错误, xml2js未找到xml2js

import xml2js from 'xml2js';

How to import JavaScript modules present in node_modules directory in TypeScript? 如何导入TypeScript的node_modules目录中存在的JavaScript模块?

You need to add a reference path 您需要添加参考路径

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

Remember you need to have a typescript module of xml2js 记住,您需要一个xml2js的打字稿模块

tsd install xml2js

I think you need to install or at least write a Module declaration. 我认为您需要安装或至少编写一个Module声明。 Here is the declaration file on definitely typed: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/xml2js/xml2js.d.ts 这是绝对类型的声明文件: https : //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/xml2js/xml2js.d.ts

You can install these module declarations with typings. 您可以使用类型安装这些模块声明。 https://github.com/typings/typings https://github.com/typings/typings

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

You can also write your own module declarations but I recommend to look first in the registries. 您也可以编写自己的模块声明,但我建议先在注册表中查看。 TypeScript will not understand any module without a declaration file. 没有声明文件,TypeScript将无法理解任何模块。

Hope that helps ;) 希望能有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM