Search code examples
node.jsclasspathrequirenode-modules

Correct path notation to access custom module from another custom module in node.js


I have some custom class modules in my node.js project. Is it possible to access a custom module from another custom module? If so, what path notation can I use? I can't seem to come up with one that doesn't produce the error: Error: Cannot find module 'someModules/DB'. I can export and require('./someModules/DB') fine from my root index.js script. But it doesn't work from someModules/Table. Here's my directory structure:

project root
  -index.js
  -someModules
    --DB
      ---index.js
    --Table
      ---index.js

I want to access DB/index.js from Table/index.js. I tried obvious things like:

const DB = require("/someModules/DB");

const DB = require("./someModules/DB");

const DB = require("../someModules/DB");

const DB = require("someModules/DB");


Solution

  • Try this. You dont need to and arent going higher up them someModules

      const DB = require("../DB");