2017-03-11 47 views
0

我使用yeoman生成了快速打字稿項目,並且隨時運行應用程序,得到ff錯誤: 找不到模塊「morgan」 找不到模塊「解析器」 找不到模塊‘餅乾分析器’在Yeoman生成的快速打字稿項目中找不到模塊錯誤

但是,這所有模塊退出在node_modules目錄,我用Google搜索周圍,我能找到的唯一的事情就是運行在根NPM鏈接(模塊名)沒有括號該項目,但仍然存在的問題,我已經嘗試npm安裝在根和錯誤不會消失。我也在本地安裝了那些缺少的模塊,但它仍然不起作用。

我做錯了什麼。

這是我的app.ts.

/// <reference path="./typings/tsd.d.ts"/> 
/// <reference path="./typings/index.d.ts" /> 

import * as path from 'path'; 
import * as logger from 'morgan'; 
import * as express from 'express'; 
import * as bodyparser from 'body-parser'; 
import * as cookieParser from 'cookie-parser' 

// Import our application router class to handle routing. 
import { ApplicationRouter } from './routes/index'; 

// Module for the express application. 
var app = express(); 

// Our express middleware. 
app.use(logger('dev')); 
app.use(bodyparser.json()); 
app.use(bodyparser.urlencoded({ extended: false })); 
app.use(cookieParser()); 

// Global application headers. 
app.use((req: express.Request, res: express.Response, next: Function) => { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Method', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); 
    res.header('Access-Control-Allow-Header', 'Origin, X-Requested-With, Content-Type, Accept'); 
}); 

// Router Module 
let appRouter = new ApplicationRouter(); 

// Application's routes. 
app.use(appRouter.getIndex()); 

// Catch 404 and forward to error handler. 
app.use((req: express.Request, res: express.Response, next: Function) => { 
    var error: any = new Error('Not Found'); 
    error.status = 404; 
    next(error); 
}); 

// Development error handler will print stacktrace. 
if (app.get('env') === 'development') { 
    app.use((error: any, req: express.Request, res: express.Response, next: Function) => { 
    return res.status(error.status || 500); 
    }); 
} 

// Production error handler prints no stacktrace to user. 
app.use((error: any, req: express.Request, res: express.Response, next: Function) => { 
    return res.status(error.status || 500); 
}); 

module.exports = app; 

回答

0

Typescript需要與這些模塊關聯的定義文件。 該文件通常是社區維護和提供的DefinitelyTyped Github Site

由於typescript 2.0,可以將它們添加到項目使用npm。

要爲摩根安裝那些例如只需要運行的每一個模塊

npm install @types/morgan 

等(避免使用/// <reference path=元標記,如果有需要作出定義文件可用,這不是通過npm可用的 - 比如自己製作的 - 只需使用tsconfig.json文件並確保typings目錄不會從編譯路徑中排除)

+0

這是半正確的,但r參考標籤與npm無關,文件如何分發,或者您是否自己編寫。當你指出時,它必須執行tsconfig.json文件,更重要的是,所描述的庫是否是一個模塊。換句話說,如果你自己編寫文件,如果它是一個模塊,你仍然不會使用引用標籤 –

相關問題