2016-03-02 30 views
1

使用require打印打印稿和标准NPM模块的最佳方法是什么? 我试图使用debug包。 我从故宫 安装也tsd install debug如何在打字稿中导入/需要标准节点模块?

然而,相同的语法是确定在一个文件中,但不是在另一个。 我想这是一个加载顺序的东西,TS认为我正在重新声明一个变量?上左/右面板

let debug = require("debug")("async-test"); 
# ReferenceError: debug is not defined 

debug = require("debug")("async-test"); 
# ReferenceError: debug is not defined 

相同代码(不同的文件)将显示一个错误/不。

enter image description here

+1

尝试'进口*从 “调试” 调试;'和那么'const test = debug(“async-test”);'这是否仍然会给你错误? – rgvassar

回答

4

什么是使用具有打字稿和标准NPM模块需要的最佳方式?

typings一个去。它有很大的debug定义https://github.com/typed-typings/npm-debug

npm install typings -g 
typings install debug 

然后设置您的tsconfig.jsonhttps://github.com/typings/typings#maindts-and-browserdts

现在,你可以这样做:

import debug = require('debug') 

有了完整的类型安全

+0

这帮了很多,谢谢! – dcsan

+0

我有这个问题,但错误是'TS2307:找不到模块'调试'...这是与TypeScript 2.0和类型的地方。任何想法,为什么我会得到这个消息? – ken