模块没有导出成员 'Request'。

6
我看到有很多关于这个主题的帖子,但是我没有看到任何与Express和我的特定问题相关的帖子,我的问题是:
import * as express from 'express';
import { Request, Response, Application } from 'express';  // <-- Error here

当我使用'Request''Response''Application'时,出现了错误(Module has no imported member)。

我发现在node_modules中的/lib子目录中有request.jsresponse.jsapplication.js。根据错误跟踪,我猜测Express没有检查/lib子目录。在使用import时,如何强制/指导系统检查/lib子目录?(或者这不是问题,而是完全不同的问题?)

我尝试过import { Request, Response, Application } from 'express/lib'import * as expressLib from 'express/lib',然后再使用import {Request, Response, Application } from expressLib,但这两种方法都不起作用。


注意:因为RequestResponse是Object类型,所以我假设它们应该保持大写?

import * as express from 'express';
// import * as expressLib from 'express/lib'
import { Request, Response, Application } from 'express';
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
import * as jwt from 'jsonwebtoken';
import * as fs from "fs";

const app: Application = express();

app.use(bodyParser.json());

app.route('api/login')
    .post(loginRoute);

const RSA_PRIVATE_KEY = fs.readFileSync('/demos/private.key');


export function loginRoute(req: Request, res: Response) {

    const email = req.body.email, 
        password = req.body.password;

        if (validateEmailAndPassword()) {
            const userId = findUserIdForEmail(email);

            const jwtBearerToken = jwt.sign({}, RSA_PRIVATE_KEY, {
                algorithm: 'RS256',
                expiresIn: 120,
                subject: userId
            });
            // res.cookie("SESSIONID", jwtBearerToken, {httpOnly: true, secure: true});

            res.status(300).json({
                idToken: jwtBearerToken,
                expiresIn: ""
            })
        } else {
            res.sendStatus(401);
        }
}

2个回答

5

更新:我没有意识到你使用typescript,请忽略原始答案。

如果您安装了@types/express,则在typescript中,大写的导入应该可以工作 - 因为此软件包导出了大写的类型: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L86

可能在重启之前,类型软件包没有被索引。这是我唯一的猜测,考虑到问题已经解决。

=======================================

当一个模块被导入时,列在 main 中的文件会加载 package.json 文件。如果 package.json 中没有 main 文件,则会加载模块根目录下的 index.js
对于 express ,没有 main 文件,index.js 会加载 ./lib/expresshttps://github.com/expressjs/express/blob/master/index.js
module.exports = require('./lib/express');

通过检查./lib/express,我们可以看到它以小写形式导出requestresponseapplicationhttps://github.com/expressjs/express/blob/master/lib/express.js#L59

/**
 * Expose the prototypes.
 */

exports.application = proto;
exports.request = req;
exports.response = res;

因此,为了导入它们,您应该使用小写字母:

import { response, request, application } from 'express'; 

非常感谢您的回答。但是,现在我真的很困惑。我刚刚重新启动了我的计算机,没有进行任何更改,现在令人惊讶的是,使用大写字母的import { Request, Response, Application } from 'express'有效了。另外,我编辑了我的原始帖子,以提供更多关于为什么我认为这些应该是大写字母的细节。非常感谢您的查看。 - Crowdpleasr
1
@Crowdpleasr 嗨,谢谢你的回复。我没有意识到你在使用 TypeScript,对混淆感到抱歉。已更新答案。 - antonku

2

您想将'Request'、'Response'和'Application'转换为小写。

当我运行此代码时:

console.log('a',express.application);
console.log('A',express.Application);

console.log('req',express.request);
console.log('Req',express.Request);

console.log('res',express.response);
console.log('Res',express.Response);

我在'A'、'Req'和'Res'处得到了undefined

(我本想把这个作为评论留下,但是我无法留下评论。)


非常感谢您的回复。请查看我对原帖的编辑。 - Crowdpleasr
1
老实说,当我运行 node index.js 时,第一行就出现了错误:import * as express from 'express'; ^ SyntaxError: Unexpected token *你能否把你的代码和具体的错误信息发给我看看呢? 或者也可以附上你正在尝试跟随的示例链接。 - shanecandoit
以上基本上是完整的代码,添加到我的 OP 中。我试图遵循的文章链接是:https://blog.angular-university.io/angular-jwt-authentication/。重新启动解决了问题,这太奇怪了,按照你和 @antonku 的故障排除步骤,请求(Request)和响应(Response)应该是小写的,但不知何故大写时突然就能工作了。非常令人困惑! - Crowdpleasr
我无法再为您提供更多帮助了。我在第一行遇到了一个错误。如果没有更多的帮助,我无法做得更好。如果您发布确切的代码和确切的错误,互联网上的帮助机器基本上可以编写任何您想要的代码。 ;) - shanecandoit
抱歉,我的错。我以为我已经贴上了完整的代码,但其实我没有保存它。我刚刚保存好了。 - Crowdpleasr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接