[英]Can't Find Modules Error In Yeoman Generated Express Typescript Project
I generated an express typescript project using yeoman and anytime i run the application, the get the ff errors: Cannot find module "morgan" Cannot find module "body-parser" Cannot find module "cookie-parser" 我使用yeoman生成了一个快速打字稿项目,并且每次运行该应用程序时,都会得到以下ff错误:找不到模块“ morgan”找不到模块“ body-parser”找不到模块“ cookie-parser”
But all this modules exits in the node_modules directory, i googled around and the only thing i could find was to run npm link (modulename) without the braces at the root of the project but still the problem exists, I've tried npm install at the root and the error doesnt go away. 但是所有这些模块都在node_modules目录中退出,我在Google上四处搜索,唯一能找到的是运行npm链接(模块名),而在项目的根目录没有大括号,但问题仍然存在,我尝试在npm install根和错误不会消失。 I have also installed just those missing modules locally and it still doesnt work.
我也只在本地安装了那些缺少的模块,它仍然无法正常工作。
What am i doing wrong. 我究竟做错了什么。
This is my app.ts. 这是我的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;
Typescript needs the definition files associated with these modules. Typescript需要与这些模块关联的定义文件。 The files are usually community maintained and made available on the DefinitelyTyped Github Site
这些文件通常由社区维护,并在DefinitelyTyped Github网站上可用
Since typescript 2.0, it is possible to add them to the project using npm. 从typescript 2.0开始,可以使用npm将它们添加到项目中。
To install those for morgan for instance simply run 例如,要为摩根安装它们,只需运行
npm install @types/morgan
and so on for every module 对每个模块等等
(avoid using the /// <reference path=
meta-tag. If there is a need to make definitions files available, which are not available through npm - such as the one crafted by yourself - simply use a tsconfig.json
file and make sure the typings
directory is not excluded from the compilation path) (避免使用
/// <reference path=
元标记。如果需要使定义文件可用,而npm无法提供这些定义文件(例如您自己制作的文件),只需使用tsconfig.json
文件并确定typings
目录不会从编译路径除外)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.