250

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

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

12 Answers 12

345

Try this:

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

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

5
  • 28
    I had been trying that with no luck but I just did fs.readFile(__dirname + '/../../foo.bar'); and it worked.
    – fancy
    Aug 16, 2011 at 18:30
  • 6
    I am assuming then that __dirname was somthing like '/foo/bar' rather than '/foo/bar/'. Aug 16, 2011 at 18:31
  • 4
    The very first '/' in your path: '/../../foo.bar' is crucial. I had '../../foo.bar' which was causing my issue. Dec 3, 2016 at 15:02
  • 1
    why? just generally why? can someone please explain?
    – eyurdakul
    May 16, 2017 at 15:12
  • @eyurdakul If I understand it corrently: __dirname may look like /path/to/your/dir, if you say __dirname + ".." it is /path/to/your/dir.., which is a nonexistent directory, rather than /path/to/your. The slash is important.
    – joulev
    Apr 4, 2020 at 3:51
215

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.

3
  • 5
    this answer with path.join is the correct way, the accepted answer should not be followed, it even triggers eslint on airbnb-base preset, the rule no-path-concat in particular
    – revelt
    Oct 15, 2017 at 22:03
  • 100th upvote. My __dirname was host/src/folder and I needed host/folder and this worked for me, not the OP answer.
    – carkod
    Jan 24, 2018 at 14:58
  • 9
    you can take it one step further and do path.join(__dirname, '..', '..', 'foo.bar')
    – Mr. Nobody
    Aug 26, 2018 at 20:26
104

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

3
  • 4
    'foo.bar" should be 'foo.bar'. I tried to make an edit but edits need to be 6 characters minimum (stupid rule if you ask me, prevents us from editing small typos like this). Dec 29, 2014 at 20:21
  • 1
    I suspect that this is the best answer. Some of the other answers might work for a given individual on a given operating system, but the presence of a specific kind of file hierarchy separator (i.e. the slash) in those other answers makes me wonder how universal they are. e.g. I'm trying to write an Electron app in a platform agnostic manner and, while I haven't exhaustively proved it, I suspect this is the safest way. Thanks. Apr 20, 2018 at 23:32
  • This is actually unnecessary, as path.join() internally uses path.normalize() (which transforms all path separators to the current/intended OS format) on the resulting joined path before returning. It can't hurt, though.
    – iono
    Sep 29, 2018 at 6:21
63

The easiest way would be to use path.resolve:

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

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"));
12

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.

8

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');
5

You can use

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

this will also work:

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

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, '..', '..') + '/'

2

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'))
2

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'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.