Node require absolute path

I have a directory that looks like this:

-- app/
   |- models/
      |- user.js
   |- config.json

I want my user.js file to require config.json. Right now I'm using require('/config') but this is not working. What am I doing wrong? I'd like to avoid using require('../config').



The simple answer is that you aren't doing anything wrong. Per a little research, the require function looks for one of:

  • a core module such as fs
  • a relative filepath that you specify in your require call
  • a directory search for the appropriately named module in a node_modules folder somewhere in a parent directory of the file in which the require function is called.

See: And:

Both resources above reference the ability to also modify a NODE_PATH environment variable, however this sounds like very bad practice and at a minimum would make your code way less portable. It also probably wouldn't even work for a file like config.json since, though I'd never done it, I'd imagine changing the NODE_PATH variable only changes where require() looks for package.json files corresponding to real modules.

In summary, see: How to make the require in node.js to be always relative to the root folder of the project? as referenced above by Piotr Kowalczuk. Per that post, you have two real options:

  1. Use relative file paths.
  2. Package up your desired resource as a real module with a package.json file and drop it into the node_modules folder.

Finally, just bear in mind that what you are trying to do goes against the grain of the program that you are using. I think this is one case where you will ultimately make your life easier (and your collaborators'!) by going with the grain of Node and using relative file paths as the design intends.


I couldn't find any better solution than to write own module ????.

So, I like clean code and such an ugly path inside the require was killing me. I found many solution how to fix it, but what I liked was perfect under Linux systems but not for Windows. Then I decide to fix it.

Sexy require ????????????????????????

After testing it in some projects I have decided to make it open source ????.

const user = require('../../../database/user'); // ???? what you have
// OR
const user = require('$db/user'); // ???? no matter how deep you are
const product = require('/database/product'); // ???? alias or pathing from root directory

Getting started

Three simple steps to use it.

  1. Install the package: npm install sexy-require --save

  2. Include require('sexy-require') once on the top of your main application file.

    const routers = require('/routers');
    const api = require('$api');
  3. Optional step. Path configuration can be defined in .paths file on root directory of your project.

    $db = /server/database
    $api-v1 = /server/api/legacy
    $api-v2 = /server/api/v2

List of paths

Anywhere in your project you can get the defined shortcut paths:

    const path = require(`sexy-require`);
    console.log(path.$db); // -> '/full/path/to/app/server/database'

It seems like the module-loading functionality prepends node_modules/ to the argument. For example, place the following script into /tmp/t.js:

meow = require('meow/meow.js');

create (mkdir) the /tmp/node_modules/ and try running it with BSD's ktrace (or Linux' strace, which provides comparable functionality). Node will attempt to open the following:

  • /tmp/node_modules/meow/meow.js
  • /tmp/node_modules/meow/meow.js.js
  • /tmp/node_modules/meow/meow.js.json
  • /tmp/node_modules/meow/meow.js.node
  • /tmp/node_modules/meow/meow.js/package.json
  • /tmp/node_modules/meow/meow.js/index.js
  • /tmp/node_modules/meow/meow.js/index.json
  • /tmp/node_modules/meow/meow.js/index.node

If you do not have the node_modules/ subdirectory next to your script, it will not lookup anywhere relative to your script at all.

global.__require = function (file) {
  return require(__dirname + '/' + file)

Place this at the top of your entry point file (e.g,. index.js). Then you can just use

const foo = __require('src/foo').

I'm using the double underscore, as an ode to __dirname but you can rename it anything!


I dont think we need to discuss about this problem. It is design this way for easy of developer.
If we require /config.json maybe there are alot config.json in the sub folder which will typically make it even harder to understand

-- app/
   |- controller
      |- config.json(2) 
   |- models/
      |- user.js
      |- config.json(3)
   |- config.json(1)

Now tell me how you know which one config file you need to choose.
Solution: user.js

var config1 = require('../config.json');
var config2 = require('../controller/config.json');
var config3 = require('./config.json');

if there are only one config you better put in the global variable and use it from there.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.