2016-06-22 136 views
0

我試圖讓打字稿及lodash在我的應用程序工作,但是當我運行打字稿+ lodash - 找不到模塊

tsc --module commonjs lodash-test.ts

我總是讓

lodash-test.ts(2,20): error TS2307: Cannot find module 'lodash'.

這仍然產生lodash -test.js文件

"use strict"; 
//lodash test that will be injected in index.html 
var _ = require("lodash"); 
console.log('This is your lodash ts file'); 
console.info(_.chunk(['a', 'b', 'c', 'd'], 2)); 

現在,當我在瀏覽器中運行我的index.html文件,我越來越

lodash-test.js:3 Uncaught ReferenceError: require is not defined

在文件的第3行。

的Index.html文件

<!DOCTYPE html> 
<html>  
<head> 
    <meta charset=utf-8 /> 
    <title></title> 
    <script type="text/javascript" src="bower_components/lodash/dist/lodash.min.js"></script> 
    <script type="text/javascript" src="lodash-test.js"></script>  
</head>  
<body> 
    <h3>Hey</h3> 
</body>  
</html> 

我有我的分型文件夾中的lodash類型定義。我怎樣才能確保Typescript可以找到這個lodash/index.d.ts文件?另外我該如何解決「需求未定義」的錯誤?

這是我的文件結構:

enter image description here

感謝您的幫助

+0

嘗試導入'd.ts'類型文件與'///參考路徑=「」/>'命令在您的TypeScript文件的頂部。因爲現在'tsc'現在已經知道你有類型文件。所以它找不到它。 – Blackus

+0

@Blackus我已將我的lodash-test.ts更改爲http://imgur.com/n8Kp5pv,並生成了圖像右側的內容。我還刪除了index.html中的