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

333
Vistas
require('./file') is undefined in one file, but not others

I have an npm project I am working on and am having a strange issue with require. I have paths in one file and am using them like, const paths = require('./paths'); but I have one file where paths ends up as undefined.

Project Directory

my-project
- main.js
- a.js
- b.js
- paths.js

main.js

const a = require('./a');
const b = require('./b');
const paths = require('./paths');

async function mainFunction() {
    console.log(paths); // no problem

    await a.process();
    await b.process();
}

mainFunction();

a.js

const paths = require('./paths');

async function process() {
    console.log(paths); // no problem
}

module.exports.process = process;

b.js - doesn't work

const paths = require('./paths');

async function process() {
    console.log(paths); // undefined
}

module.exports.process = process;

b.js - does work

async function process() {
    const paths = require('./paths');
    console.log(paths); // no problem
}

module.exports.process = process;

paths.js

thePaths = {
    pathOne: '/the-path',
    // etc.
};

module.exports = {thePaths};

Can anyone explain what is happening?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I created a new file with a different name and copied the contents into it. Turns out I had another file with const b = require(./b); that wasn't using it and it was causing the problem. Definitely an odd-ball.

c.js

const b = require('./b'); // removed to resolve issue
// other requires

// file contents
about 4 years ago · Juan Pablo Isaza Denunciar
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