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
Related videos on Youtube
Author by
lonelymo
Updated on November 10, 2020Comments
-
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 almost 9 years
..\config\dev\foobar.json
-
AdityaParab almost 9 yearsIt would be better if you can just save your json data in
.js
file (instead of.json
) And then from the.js
filemodule.exports
it. :) -
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 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 about 4 yearsThe real reason to use .js over .json text files is really easy to explain: comments... ;)
-
-
yangsibai almost 9 yearsNo need to use
__dirname
-
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 almost 9 yearsPlease add some explanation to your code here.
-
lonelymo almost 9 yearsperfect this worked! I didn't us path but __dirname + '/..'
-
gramcha almost 7 yearsThis 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 over 6 yearsThis 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 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