Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

481
Visualizações
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()

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

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"

over 4 years ago · Santiago Trujillo Relatório

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";
over 4 years ago · Santiago Trujillo Relatório

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda