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

332
Visualizações
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 Respostas
Responde à pergunta

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 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