2016-03-02 66 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