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

328
Views
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 answers
Answer question

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