How can I import a route file using Express?

How can I import a route file using Express?


0

Normally when I make a server I have no problem importing route files but I’m working on a project using GraphQL which requires me to include "type": "module", inside my package.json file. So when I try having this inside my app.js file

const indexRouter = require("./routes/index");

It gives me this error.

ReferenceError: require is not defined in ES module scope, you can use import instead

How do I use import for a file like that? Inside that file is a bunch of router.get functions like this:

router.get("/", function (req, res, next) {
  res.render("index", { title: "Library" });
});

router.get("/library/:title", function (req, res, next) {
  res.render("library");
});

Would I have to manually export every render? Or is there a simpler solution I can’t seem to figure out?

Share
Improve this question


Load 6 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *