Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
How to require directories using async / await

I am trying to load data which is in javascript files into an object.

All the files are like this:

module.exports = {
  test: ‘qwerty’
}

I’m using require, but I have to load several directories so need to do the loads one at a time.

I tried wrapping it in a promise:

function load(fileOrDirPath) {
  return new Promise(function(resolve, reject) {
    let data;

    debug(`Requiring: [${fileOrDirPath}]`);

    try {
      data = require(fileOrDirPath);
    }
    catch(e) {
      return reject(e);
    }

    debug(`Loaded data: [${JSON.stringify(data, 0, 2)}]`);

    return resolve(data);
  });
}

I use the function:

const dirPath = ‘redacted’
const data = await load(dirPath);
debug(`Loaded: [${JSON.stringify(data, 0, 2)}]`);

And the logs show that the data is loaded inside the function. However outside the data is always null.

Why doesn’t the function await?

I tried looking on npm for a module but couldn’t find any.

How can I load a directory of javascript files recursively into an object?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda