2013-12-12 245 views
1

我有一個導出的模塊在一個文件(upload.ts)在Typescript中,我無法導入到另一個文件(application.ts)沒有錯誤。另外,我無法導入ExpressJS。Typescript無法找到模塊

這裏的Upload.ts

/// <reference path="Main.d.ts" /> 

var fs = require('fs'); 
var path = require('path'); 
var formidable = require('formidable'); 

export class Upload{ 

    public parse(req, res, next) { 
     ... 
    } 

    public save(req, res, next) { 
     ... 
    } 

    public respond(req, res, next) { 
     ... 
    } 

    public errors(err, req, res, next) { 
     ... 
    } 
}; 

這裏的application.ts

/// <reference path="Main.d.ts" /> 

var http = require("http"); 
import express = require("express"); 
import upload = require("Upload"); 
var upload = new upload.Upload(); 

var app = express.express(); 
var HOST = "localhost"; 
var PORT = 8080; 

app.use(express.logger()); 
app.use(app.router); 

var app = express.express();代碼拋出一個錯誤:未解決的函數或方法明示。我已經導入express.d.ts從https://github.com/borisyankov/DefinitelyTyped/tree/master/express

這裏的錯誤:

module.js:340 
    throw err; 
     ^
Error: Cannot find module 'upload' 
    at Function.Module._resolveFilename (module.js:338:15) 
    at Function.Module._load (module.js:280:25) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    at Object.<anonymous> (C:\Users\Me\WebstormProjects\untitled\server\main.js:12:14) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Function.Module.runMain (module.js:497:10) 

這裏的頭文件,它引用到兩個:

/// <reference path="upload.ts" /> 
/// <reference path="node.d.ts" /> 
/// <reference path="application.ts" /> 
/// <reference path="express.d.ts" /> 

回答

7

您需要:

  • 請務必將express.d.tsnode.d.ts放到您的項目目錄中,靠近你讀應用程序文件
  • 將其導入到你的應用程序

例子:

/// <reference path="libs/express.d.ts" /> 
/// <reference path="libs/node.d.ts" /> 

import http = require("http"); 
import express = require("express"); 
import upload = require("Upload"); 

var upload = new upload.Upload(); 
var app = express(); 

Upload模塊能夠以類似的方式被引用。 順便說一句,你的代碼看起來更像JavaScript然後打字稿

+0

其實我已經這樣做了。有一個共同的頭文件,所有文件都提到並指向所有文件。我正在從Javascript轉換到Typescript – EternallyCurious