在Yeoman生成的Express Typescript项目中无法找到模块错误

时间:2017-03-11 10:49:41

标签: node.js express typescript yeoman-generator

我使用yeoman生成了一个快速打字稿项目,无论何时我运行应用程序,都会得到ff错误: 找不到模块" morgan" 找不到模块" body-parser" 找不到模块" cookie-parser"

但是所有这些模块都在node_modules目录中退出,我用Google搜索,我唯一能找到的就是运行npm link(modulename)而没有项目根目录的大括号,但问题仍然存在,我'我试过根目录下的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;

1 个答案:

答案 0 :(得分:0)

Typescript需要与这些模块关联的定义文件。 这些文件通常由社区维护,并在DefinitelyTyped Github Site

上提供

从typescript 2.0开始,可以使用npm将它们添加到项目中。

例如,要为摩根安装那些只需运行

npm install @types/morgan
每个模块的

等等

(避免使用/// <reference path=元标记。如果需要使定义文件可用(通过npm无法使用) - 例如您自己制作的文件 - 只需使用tsconfig.json文件并确保未从编译路径中排除typings目录)