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\controllers\instructor.js:1
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\node_modules\query-string\index.js not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\controllers\instructor.js:2:21)
at Generator.next (<anonymous>)
at Object.<anonymous> (C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\routes\instructor.js:1)
at Generator.next (<anonymous>)
at C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\server.js:33:52
at Array.map (<anonymous>)
at Object.<anonymous> (C:\Users\MRPla\Downloads\CourseGuccho\edemy\server\server.js:33:25)
at Generator.next (<anonymous>) {
code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
The relevant code is as follows:
server>controllers>instructor.js
import User from "../models/user";
import stripe from "stripe";
import queryString from "query-string";
export const makeInstructor = async (req, res) => {
try {
// 1. find user from db
const user = await User.findById(req.user._id).exec();
// 2. if user dont have stripe_account_id yet, then create new
if (!user.stripe_account_id) {
const account = await stripe.accounts.create({ type: "express" });
// console.log('ACCOUNT => ', account.id)
user.stripe_account_id = account.id;
user.save();
}
// 3. create account link based on account id (for frontend to complete onboarding)
const accountLink = await stripe.accountLinks.create({
account: user.stripe_account_id,
refresh_url: process.env.STRIPE_REDIRECT_URL,
return_url: process.env.STRIPE_REDIRECT_URL,
type: "account_onboarding",
});
// console.log(accountLink)
// 4. pre-fill any info such as email (optional), then send url resposne to frontend
accountLink = Object.assign(accountLink, {
"stripe_user[email]": user.email,
});
// 5. then send the account link as response to fronend
res.send(`${accountLink.url}?${queryString.stringify(accountLink)}`);
} catch (err) {
console.log("MAKE INSTRUCTOR ERR ", err);
}
};
server>routes>instructor.js
import express from "express";
const router = express.Router();
// middleware
import { requireSignin } from "../middlewares";
// controllers
import { makeInstructor } from "../controllers/instructor";
router.post("/make-instructor", requireSignin, makeInstructor);
module.exports = router;
server>server.js
import express from "express";
import cors from "cors";
import { readdirSync } from "fs";
import mongoose from "mongoose";
import csrf from "csurf";
import cookieParser from "cookie-parser";
const morgan = require("morgan");
require("dotenv").config();
const csrfProtection = csrf({ cookie: true });
// create express app
const app = express();
// db
mongoose
.connect(process.env.DATABASE, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => console.log("**DB CONNECTED**"))
.catch((err) => console.log("DB CONNECTION ERR => ", err));
// apply middlewares
app.use(cors());
app.use(express.json());
app.use(cookieParser());
app.use(morgan("dev"));
// route
readdirSync("./routes").map((r) => app.use("/api", require(`./routes/${r}`)));
// csrf
app.use(csrfProtection);
app.get("/api/csrf-token", (req, res) => {
res.json({ csrfToken: req.csrfToken() });
});
// port
const port = process.env.PORT || 8000;
app.listen(port, () => console.log(`Server is running on port ${port}`));
Here is the package.JSON file (for server):
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1285.0",
"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": "^8.2.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.13.15",
"morgan": "^1.10.0",
"nodemon": "^2.0.20",
"query-string": "^8.1.0",
"shortid": "^2.2.16",
"stripe": "^11.5.0"
}
}
if i need to change the require of index.js in null to a dynamic import() which is available in all CommonJS modules then how i will make that work?