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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carl Brubaker