fs: how do I locate a parent folder?

Javascriptnode.jsFilesystems

Javascript Problem Overview


How do I write this to go back up the parent 2 levels to find a file?

fs.readFile(__dirname + 'foo.bar');

Javascript Solutions


Solution 1 - Javascript

Try this:

fs.readFile(__dirname + '/../../foo.bar');

Note the forward slash at the beginning of the relative path.

Solution 2 - Javascript

Use path.join http://nodejs.org/docs/v0.4.10/api/path.html#path.join

var path = require("path"),
    fs = require("fs");

fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));

path.join() will handle leading/trailing slashes for you and just do the right thing and you don't have to try to remember when trailing slashes exist and when they dont.

Solution 3 - Javascript

I know it is a bit picky, but all the answers so far are not quite right.

The point of path.join() is to eliminate the need for the caller to know which directory separator to use (making code platform agnostic).

Technically the correct answer would be something like:

var path = require("path");

fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));

I would have added this as a comment to Alex Wayne's answer but not enough rep yet!

EDIT: as per user1767586's observation

Solution 4 - Javascript

The easiest way would be to use path.resolve:

path.resolve(__dirname, '..', '..');

Solution 5 - Javascript

Looks like you'll need the path module. (path.normalize in particular)

var path = require("path"),
	fs = require("fs");
	
fs.readFile(path.normalize(__dirname + "/../../foo.bar"));

Solution 6 - Javascript

If another module calls yours and you'd still like to know the location of the main file being run you can use a modification of @Jason's code:

var path = require('path'),
    __parentDir = path.dirname(process.mainModule.filename);

fs.readFile(__parentDir + '/foo.bar');

That way you'll get the location of the script actually being run.

Solution 7 - Javascript

If you not positive on where the parent is, this will get you the path;

var path = require('path'),
    __parentDir = path.dirname(module.parent.filename);

fs.readFile(__parentDir + '/foo.bar');

Solution 8 - Javascript

You can use

path.join(__dirname, '../..');

Solution 9 - Javascript

i'm running electron app and i can get the parent folder by path.resolve()

parent 1 level:path.resolve(__dirname, '..') + '/'

parent 2 levels:path.resolve(__dirname, '..', '..') + '/'

Solution 10 - Javascript

This works fine

path.join(__dirname + '/../client/index.html')
const path = require('path')
const fs = require('fs')
    fs.readFile(path.join(__dirname + '/../client/index.html'))

Solution 11 - Javascript

this will also work:

fs.readFile(`${__dirname}/../../foo.bar`);

Solution 12 - Javascript

You can locate the file under parent folder in different ways,

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

// reads foo.bar file which is located in immediate parent folder.
fs.readFile(path.join(__dirname, '..', 'foo.bar'); 

// Method 1: reads foo.bar file which is located in 2 level back of the current folder.
path.join(__dirname, '..','..');


// Method 2: reads foo.bar file which is located in 2 level back of the current folder.
fs.readFile(path.normalize(__dirname + "/../../foo.bar"));

// Method 3: reads foo.bar file which is located in 2 level back of the current folder.
fs.readFile(__dirname + '/../../foo.bar');

// Method 4: reads foo.bar file which is located in 2 level back of the current folder.
fs.readFile(path.resolve(__dirname, '..', '..','foo.bar'));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfancyView Question on Stackoverflow
Solution 1 - JavascriptAndrew HareView Answer on Stackoverflow
Solution 2 - JavascriptAlex WayneView Answer on Stackoverflow
Solution 3 - JavascriptsmremdeView Answer on Stackoverflow
Solution 4 - JavascriptYan FotoView Answer on Stackoverflow
Solution 5 - JavascriptDominic BarnesView Answer on Stackoverflow
Solution 6 - JavascriptJeremy BattleView Answer on Stackoverflow
Solution 7 - JavascriptJason BrumwellView Answer on Stackoverflow
Solution 8 - JavascriptpuneetView Answer on Stackoverflow
Solution 9 - JavascriptグエントゥアンズンView Answer on Stackoverflow
Solution 10 - JavascriptHanzla HabibView Answer on Stackoverflow
Solution 11 - JavascriptDhruvin modiView Answer on Stackoverflow
Solution 12 - JavascriptCodemakerView Answer on Stackoverflow