I'm new to MERN development. I'm trying to build a learning management system by following an old tutorial. But I ended up getting this error:
> C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\middlewares\index.js:1
> import {expressJwt} from "express-jwt"; SyntaxError: The requested
> module
> 'file:///C:/Users/MRPla/Downloads/CourseGuccho/edemy/server/node_modules/express-jwt/dist/index.js'
> does not provide an export named 'expressJwt'
> at Object.<anonymous> (C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\middlewares\index.js:1)
> at Generator.next (<anonymous>)
> at Object.<anonymous> (C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\routes\auth.js:1)
> at Generator.next (<anonymous>)
> at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) [nodemon] app crashed -
> waiting for file changes before starting...
The relevant code is as follows:
server>middlewares>index.js:
import expressJwt from "express-jwt";
export const requireSignin = expressJwt({
getToken: (req, res) => req.cookies.token,
secret: process.env.JWT_SECRET,
algorithms: ["HS256"],
});
server>routes>auth.js:
import express from "express";
const router = express.Router();
// middleware
import { requireSignin } from "../middlewares";
// controllers
import { register, login, logout, currentUser } from "../controllers/auth";
router.post("/register", register);
router.post("/login", login);
router.get("/logout", logout);
router.get("/current-user", requireSignin, currentUser);
module.exports = router;
I have also used this format for middlewares:
1.
import {expressJwt} from "express-jwt";
export const requireSignin = expressJwt({
getToken: (req, res) => req.cookies.token,
secret: process.env.JWT_SECRET,
algorithms: ["HS256"],
});
import {expressJwt} from "express-jwt";
const requireSignin = expressJwt({
getToken: (req, res) => req.cookies.token,
secret: process.env.JWT_SECRET,
algorithms: ["HS256"],
});
export default requireSignin;
but this doesn't help at all.
Here is the package.JSON file (for server):
{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"csurf": "^1.11.0",
"dotenv": "^8.6.0",
"esm": "^3.2.25",
"express": "^4.18.2",
"express-jwt": "^7.7.7",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.13.15",
"morgan": "^1.10.0",
"nodemon": "^2.0.20"
},
"description": ""
}
The correct way to import in this case is:
import {expressjwt} from "express-jwt";
(lowercase j
)
Another thing to try in general when you encounter errors like
The requested module [...] does not provide an export named 'default'
is using a star-import :
import * as X from "x";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.