NodeJS accessing file with relative path

158,846

Solution 1

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.

Solution 2

Simple! The folder named .. is the parent folder, so you can make the path to the file you need as such

var foobar = require('../config/dev/foobar.json');

If you needed to go up two levels, you would write ../../ etc

Some more details about this in this SO answer and it's comments

Share:
158,846

Related videos on Youtube

lonelymo
Author by

lonelymo

Updated on November 10, 2020

Comments

  • lonelymo
    lonelymo over 3 years

    It seemed like a straight forward problem. But I amn't able to crack this. Within helper1.js I would like to access foobar.json (from config/dev/)

    root
      -config
       --dev
        ---foobar.json
      -helpers
       --helper1.js
    

    I couldn't get this to work fs: how do I locate a parent folder?

    Any help here would be great.

    • Ben
      Ben almost 9 years
      ..\config\dev\foobar.json
    • AdityaParab
      AdityaParab almost 9 years
      It would be better if you can just save your json data in .js file (instead of .json) And then from the .js file module.exports it. :)
    • slebetman
      slebetman almost 9 years
      @AdityaParab: If you save you JSON file as .json instead of .js then you don't need to module.export it - you can require it directly. JSON file are automatically completely exported (or to put it another way, JSON files are supported by require())
    • Bernardo Dal Corno
      Bernardo Dal Corno over 4 years
      @slebetman js files are more flexible, JSON requires double quotes, doesn't allow comments, etc.. One could use JSON5 or other similar format, but then you need a lib to read it. Js also allows dynamic data generation, from a function for example
    • jrypkahauer
      jrypkahauer about 4 years
      The real reason to use .js over .json text files is really easy to explain: comments... ;)
  • yangsibai
    yangsibai almost 9 years
    No need to use __dirname
  • AerandiR
    AerandiR almost 9 years
    @osrpt It's better to use __dirname because the path will be correct regardless of the context in which the helper1 script is run (e.g. node helpers/helper1.js will work from the root directory).
  • Shamas S
    Shamas S almost 9 years
    Please add some explanation to your code here.
  • lonelymo
    lonelymo almost 9 years
    perfect this worked! I didn't us path but __dirname + '/..'
  • gramcha
    gramcha almost 7 years
    This will only work on json/js file. It will not work on other types of files like xml. Better approach is path.join()
  • Isan Rivkin
    Isan Rivkin over 6 years
    This is not a very good solution if you are calling the file from another folder. If you structure is src->utils->someUtilFile.js and also have a second file src->logic->someLogic.js and you use ../../utils/someUtilFile.js this will work. but if you are calling someUtilFile.js from a different structure directory this will not work.
  • Programmer89
    Programmer89 about 2 years
    @AerandiR Thank u man, jesus it was so hard to find how to specific a folder regardless who is calling the main .js. THANKSS