How to Reference Files Relative to Application Root in Node.Js

Proper way to reference files relative to application root in Node.JS

Try

var templateContent = fs.readFileSync(path.join(__dirname, '../templates') + '/my-template.html', 'utf8');

NodeJS accessing file with relative path

You can use the path module to join the path of the directory in which helper1.js lives to the relative path of foobar.json. This will give you the absolute path to foobar.json.

var fs = require('fs');
var path = require('path');

var jsonPath = path.join(__dirname, '..', 'config', 'dev', 'foobar.json');
var jsonString = fs.readFileSync(jsonPath, 'utf8');

This should work on Linux, OSX, and Windows assuming a UTF8 encoding.

How to make node.js require absolute? (instead of relative)

Here is the actual way I'm doing for more than 6 months. I use a folder named node_modules as my root folder in the project, in this way it will always look for that folder from everywhere I call an absolute require:

  • node_modules

    • myProject

      • index.js I can require("myProject/someFolder/hey.js") instead of require("./someFolder/hey.js")
      • someFolder which contains hey.js

This is more useful when you are nested into folders and it's a lot less work to change a file location if is set in absolute way. I only use 2 the relative require in my whole app.

Project root based relative paths for NodeJS modules

I approached an, even not perfect, acceptable solution by using a runtime Symbol to store project's root path as a property of the process object.

Using a symbol to name that property we avoid any possibility of
colliding with another process object properties, even with future
ones.

What I did was simply adding this two lines to my app.js (which in Express projects is placed in project root directory and required from the main app entry point bin/www where I also ensured it is the first required dependency):

const $root = Symbol.for("projectRoot");                                         │     return new Promise(function (resolve, reject) {
process[$root] = __dirname;

After that, the only thing I need to do in all other modules is to repeat the first row at the very beginning:

const $root = Symbol.for("projectRoot");

...and use it in all require statements. For example:

const helper = require(process[$root]+"/lib/util/helpers.js

Maybe it is far from perfect, but it works for me...

node.js get relative to project/src path of file

You can use path.relative:

var relativePath = path.relative(process.cwd(), someFilePath);

Relative path for data file in NodeJS module

You would typically build a path for each location that is relative to the directory where your code is located and access the desired location in a relative way from that. The location of your code will be passed to the code's module as __dirname. You can then combine that relative path with __dirname to build a full path to the target location without making any assumptions about where or how the module is installed.

So, assuming your code is in the src directory, that would be where __dirname points to. To, get access to the data directory below the src directory, you would use:

let srcDataDir = path.join(__dirname, "data");

To get access to the dist/data directory, you would use:

let distDataDir = path.join(__dirname, '../dist/data');

To get access to the higher level data directory where MY_DATA_FILES.json is, you would use:

let topDataDir = path.join(__dirname, '../data');

As, you can see, the key is to build everything relative from the location you do know which is __dirname passed to the code as the location of the code's own directory.

In Javascript's modules, you don't ever want to make any assumptions about the current working directory because that can literally be anything. That's controlled by the top level program itself and how the program was started and is not anything the module itself can rely on or make assumptions about. But __dirname will always be the full path to the directory where your module's code is running from.



Related Topics



Leave a reply



Submit