首页 文章

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

提问于
浏览
0

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

但所有这些模块都在node_modules目录中退出,我用Google搜索并且我唯一能找到的就是运行npm link(modulename)而没有项目根目录的大括号但仍然存在问题,我已尝试过npm install at根和错误不会消失 . 我还在本地安装了那些缺少的模块,它仍然无法正常工作 .

我究竟做错了什么 .

这是我的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

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

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

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

    npm install @types/morgan
    

    等每个模块

    (避免使用 /// <reference path= 元标记 . 如果需要提供定义文件,这些文件不能通过npm获得 - 例如由您自己制作的文件 - 只需使用 tsconfig.json 文件并确保不排除 typings 目录编译路径)

相关问题