admin管理员组

文章数量:1289828

I see there are a ton of posts on this topic, but I didn't see any related to Express and my particular issue, which is:

import * as express from 'express';
import { Request, Response, Application } from 'express';  // <-- Error here

gives me an error (Module has no imported member for 'Request', 'Response' and 'Application').

I see that in node_modules there are the files request.js, response.js, and application.js listed in the /lib sub-directory. Based on the error trace, my guess is that Express is not checking in the /lib sub-directory. How can I force/guide the system to check in the /lib sub-directory when using import? (Or is that not the problem and there's another problem altogether?).

I tried import { Request, Response, Application } from 'express/lib', and import * as expressLib from 'express/lib and then import {Request, Response, Application } from expressLib, but neither of those worked.


Note: I have the following code below the imports . . . Because Request and Response are Object types, I assume they should be left in upper-case?

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);
        }
}

I see there are a ton of posts on this topic, but I didn't see any related to Express and my particular issue, which is:

import * as express from 'express';
import { Request, Response, Application } from 'express';  // <-- Error here

gives me an error (Module has no imported member for 'Request', 'Response' and 'Application').

I see that in node_modules there are the files request.js, response.js, and application.js listed in the /lib sub-directory. Based on the error trace, my guess is that Express is not checking in the /lib sub-directory. How can I force/guide the system to check in the /lib sub-directory when using import? (Or is that not the problem and there's another problem altogether?).

I tried import { Request, Response, Application } from 'express/lib', and import * as expressLib from 'express/lib and then import {Request, Response, Application } from expressLib, but neither of those worked.


Note: I have the following code below the imports . . . Because Request and Response are Object types, I assume they should be left in upper-case?

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);
        }
}

Share Improve this question edited Feb 13, 2022 at 10:46 halfer 20.5k19 gold badges109 silver badges202 bronze badges asked Oct 18, 2019 at 16:32 CrowdpleasrCrowdpleasr 4,0286 gold badges23 silver badges43 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Updated: I didn't realize that you use typescript, please disregard the original answer.

With typescript the capitalized imports should work if you have installed @types/express - because this package exports capitalized types: https://github./DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L86

Probably before the restart the types package has not been indexed somehow. This is the only guess that I have taking into account that the issue has been resolved.

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

When a module is imported - the file listed as the main is the package.json is loaded. If there is no main file in the package.json then index.js from the module root directory is loaded.

In case of express there is no main file and index.js loads ./lib/express: https://github./expressjs/express/blob/master/index.js

module.exports = require('./lib/express');

By checking ./lib/express we can see that it exports request, response and application in lowercase: https://github./expressjs/express/blob/master/lib/express.js#L59:

/**
 * Expose the prototypes.
 */

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

So in order to import them you should use lowercase:

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

You want to lower case 'Request', 'Response', and 'Application'.

When i run this code:

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);

I get undefined for the 'A', 'Req', and 'Res'.

(I would have made this a ment, but I cannot leave ments.)

本文标签: javascriptModule has no exported member39Request39Stack Overflow