Sergey Piterman
Sergey Piterman

Reputation: 21

How do I require JS files within the same directory properly?

I'm having trouble understanding how require() works in JS.

Here's the issue I'm having:

My folder structure looks like this:

test

  ->test

    ->a.js

    ->b.js

    ->c.js

And here is the code I have in each file:

In c.js:

function c() {
  console.log("c")
}

module.exports = c;

In b.js:

let c = require("./c");

function b() {
  c();
  console.log("b");
}

exports.b = b;

In a.js:

let b = require("./test/b")

When I execute the code in a.js, it runs fine.

But when I execute the code in b.js it throws an error:

module.js:472
    throw err;
    ^

Error: Cannot find module './c'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at [stdin]:3:9
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at Object.runInThisContext (vm.js:95:38)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:571:32)
    at evalScript (bootstrap_node.js:391:27)

But when I change the contents of b.js to be:

let c = require("./test/c");

function b() {
  c();
  console.log("b");
}

module.exports = b;

Now a.js throws an error:

module.js:472
    throw err;
    ^

Error: Cannot find module './test/c'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/sp/Desktop/test/test/b.js:1:71)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

What am I not understanding?

Upvotes: 2

Views: 16647

Answers (2)

dnc
dnc

Reputation: 620

I assume you're running them all from /test/ root directory.

Running a.js from root: ./test/b points to /test/test/b

Running b.js from root: ./c points to /test/c which does not exist.

When you run a.js from root, it requires b.js from ./test/b which then requires c.js going the relative path from its own directory via ./c - that's why it's working if you run a.js.

Now running b.js from upper /test/ directory results in b.js looking for c.js in /test/c instead of /test/test/c.

Changing b.js to let c = require("./test/c"); leads to the following:

a.js still requires b.js from ./test/b aka /test/test/b. Now b.js tries to require from ./test/c pointing to non existent /test/test/test/c.

Upvotes: 1

David Alsh
David Alsh

Reputation: 7599

Node looks for a module object from a file it require()s. Specifically looking at module.exports

// File a.js
const foo = "Hello"
module.exports = foo

 

// File b.js
const bar = "World"
module.exports = bar

 

// File index.js
const a = require('./a')
const b = require('./b')

console.log(a, b) //Hello World

You can also export objects

module.exports = { foo, bar }

Which you import like this

const { foo, bar} = require("./file")

And when you don't specify a location, it looks in node_modules

const express = require('express') // Looks inside node_modules for express

Upvotes: 0

Related Questions