我是MERN开发的新手。我试图通过遵循旧教程来建立一个学习管理系统。但是我最终得到了这个错误:
> ..servermiddlewaresindex.js:1
> import {expressJwt} from "express-jwt"; SyntaxError: The requested
> module
> ../server/node_modules/express-jwt/dist/index.js'
> does not provide an export named 'default'
> at Object.<anonymous> ..servermiddlewaresindex.js:1)
> at Generator.next (<anonymous>)
> at Object.<anonymous> ..serverroutesauth.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...
相关代码如下:
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;
我也将这种格式用于中间件:
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;
但是这一点用都没有
这是包裹。JSON文件(用于服务器):
{
"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": ""
}
在这种情况下,正确的导入方法是:
import {expressjwt} from "express-jwt";
(小写j
)
当你遇到像
这样的错误时,另一个可以尝试的方法是请求的模块[…]]没有提供名为"default"的导出
使用star-import:
import * as X from "x";