253

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

fs.readFile(__dirname + 'foo.bar');
nbro
  • 15,395
  • 32
  • 113
  • 196
fancy
  • 48,619
  • 62
  • 153
  • 231

12 Answers12

349

Try this:

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

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

Clint
  • 2,696
  • 23
  • 42
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 28
    I had been trying that with no luck but I just did `fs.readFile(__dirname + '/../../foo.bar');` and it worked. – fancy Aug 16 '11 at 18:30
  • 6
    I am assuming then that `__dirname` was somthing like `'/foo/bar'` rather than `'/foo/bar/'`. – Andrew Hare Aug 16 '11 at 18:31
  • 4
    The very first '/' in your path: '/../../foo.bar' is crucial. I had '../../foo.bar' which was causing my issue. – levibostian Dec 03 '16 at 15:02
  • 1
    why? just generally why? can someone please explain? – eyurdakul May 16 '17 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 04 '20 at 03:51
218

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.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 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](https://eslint.org/docs/rules/no-path-concat) in particular – revelt Oct 15 '17 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 '18 at 14:58
  • 9
    you can take it one step further and do `path.join(__dirname, '..', '..', 'foo.bar')` – Mr. Nobody Aug 26 '18 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

smremde
  • 1,800
  • 1
  • 13
  • 25
  • 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). – user1767586 Dec 29 '14 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. – Andrew Willems Apr 20 '18 at 23:32
  • This is actually unnecessary, as path.join() [internally uses path.normalize()](https://nodejs.org/api/path.html#path_path_join_paths) (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 '18 at 06:21
63

The easiest way would be to use path.resolve:

path.resolve(__dirname, '..', '..');
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
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"));
Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
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.

Jeremy Battle
  • 1,638
  • 13
  • 18
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');
Jason Brumwell
  • 3,482
  • 24
  • 16
5

You can use

path.join(__dirname, '../..');
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
puneet
  • 116
  • 1
  • 2
2

this will also work:

fs.readFile(`${__dirname}/../../foo.bar`);
Dhruvin modi
  • 613
  • 1
  • 8
  • 13
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'))
Hanzla Habib
  • 3,457
  • 25
  • 25
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'));
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81