2017-03-11 4 views
0

yeoman을 사용하여 익스프레스 타이 스크립트 프로젝트를 생성했는데 응용 프로그램을 실행할 때마다 ff 오류가 발생 함 : 모듈 "morgan"을 찾을 수 없음 모듈 "body- 구문 분석기 " "쿠키 파서 "모듈을 찾을 수 없습니다Yeoman 생성 Express Typescript 프로젝트에서 모듈 오류를 찾을 수 없음

하지만이 모든 모듈은 node_modules 디렉토리에서 종료됩니다. 내가 찾은 유일한 해결책은 루트의 중괄호없이 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을 사용하여 프로젝트에 추가 할 수 있습니다.

간단합니다 (/// <reference path= 메타 태그를 사용하지 마십시오. 정의를 만들 필요가 가능한이 파일이 있으면 모든 모듈

에 대한

npm install @types/morgan 

등을 실행 예를 들어 모건 위해 사람들을 설치하려면, npm을 통해 사용할 수없는 파일 - 예 : tsconfig.json 파일을 사용하고 typings 디렉토리가 컴파일 경로에서 제외되어 있지 않은지 확인하십시오.

+0

이것은 반반이지만 r eference tag는 npm, 파일 배포 방법, 또는 직접 쓰는 지 여부와 아무 관련이 없습니다. 설명하는대로 tsconfig.json 파일을 수행해야하며 더 중요한 것은 설명 된 라이브러리가 모듈인지 여부입니다. 다른 말로하면 파일을 직접 작성한 경우 모듈 인 경우 여전히 참조 태그를 사용하지 않을 것입니다 –

관련 문제