简体   繁体   English

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

[英]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.

相关问题 使用TypeScript Node + Express项目模块中的TypeScript文件的正确方法? 当前抛出的错误:找不到模块 - Correct way of using typescript files from modules in typescript node+express project? currently throwing Error: Cannot find module 打字稿找不到node_modules - typescript can't find node_modules 约曼找不到发电机 - Yeoman can't find generators Yeoman错误,无法访问Yeoman,问题:“错误:找不到模块&#39;cli-width&#39;” - Yeoman Errors, Can't Access Yeoman, Issue: “Error: Cannot find module 'cli-width'” 找不到“EJS”打字稿和快递 - Can not find 'EJS' Typescript and Express Express更新到3.x后找不到我的节点模块 - Express can't find my node modules after update to 3.x NOde.js / Express App找不到某些node_modules - NOde.js/Express App can't find some node_modules NestJs 找不到模块 platform-express,即使它安装在 node_modules 中 - NestJs can't find module platform-express even though it's installed in node_modules 无法调试我的ts-node express项目(断点被忽略,因为未找到生成的代码) - Can't debug my ts-node express project (Breakpoint ignored because generated code not found) 我可以在Node.js / Express中动态导入TypeScript模块吗? - Can I dynamically import TypeScript modules in Node.js/Express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM