Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
how to convert the following syntax into an es6 syntax?

I have this statement to modularize my express.js api's:

app.use("/api/users", require("./routes/api/users"));

How to write it using import statement?

inside "./routes/api/users", I am exporting express.Router()

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Inside users.js use const router = express.Router() and then export default router At the top of your file write import router from "./routes/api/users"

about 4 years ago · Juan Pablo Isaza Report

0

You can use default export of es6 in "./routes/api/users" as well although it's not compulsory.

./routes/api/users


import { Router } from "express";

const router = Router();

router.get("/", (req, res)=> {
    // 
});

export default router;

And then import it using the following syntax.

import UserRoutes from "./routes/api/users";
about 4 years ago · Juan Pablo Isaza Report

0

I guess you are using node JS. So, basically nodeJS uses CommonJS by default, so you are using require() statement which is a part of CommonJS. To use the latest ES6 import export feature, go to package.json and add the following inside of the JSON container:

    "type": "module",

Note that this must be in the first object, not nested inside of anything. You can write it below the version ( for ease of understanding ).

Hope it helps

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!