194

Suppose I have a file at the root of my project called file.xml.

Suppose I have a test file in tests/ called "test.js" and it has

const file = fs.readFileSync("../file.xml");

If I now run node ./tests/test.js from the root of my project it says ../file.xml does not exist. If I run the same command from within the tests directory, then it works.

It seems fs.readFileSync is relative to the directory where the script is invoked from, instead of where the script actually is. If I wrote fs.readFileSync("./file.xml") in test.js it would look more confusing and is not consistent with relative paths in a require statement which are file relative.

Why is this? How can I avoid having to rewrite the paths in my fs.readFileSync?

Soviut
  • 88,194
  • 49
  • 192
  • 260
Arman
  • 2,665
  • 3
  • 14
  • 19
  • 6
    `__dirname` is not necessarily the same as the current working directory, but `fs.readFileSync()` uses the current working directory. The current working directory is likely set to whatever it was when you launched node. – jfriend00 Jun 17 '17 at 06:11

3 Answers3

317

You can resolve the path relative the location of the source file - rather than the current directory - using path.resolve:

const path = require("path");
const file = fs.readFileSync(path.resolve(__dirname, "../file.xml"));
cartant
  • 57,105
  • 17
  • 163
  • 197
  • 13
    Because relative paths are resolved using the current directory. And you want to resolve using a different directory. From the [docs](https://nodejs.org/api/fs.html): *"The relative path to a filename can be used. Remember, however, that this path will be relative to process.cwd()."* – cartant Jun 17 '17 at 05:25
  • for some reason I got it into my head that because local file wasn't working on my remote environment that i . would need to get the file by a full url. However, of course I didn't and I was able to use this method to resolve the absolute path on my remote server.. – weaveoftheride Apr 03 '19 at 09:08
  • 2
    Note - [`path`](https://nodejs.org/api/path.html) is a node module that provides utilities for working with file and directory paths. [`__dirname`](https://www.digitalocean.com/community/tutorials/nodejs-how-to-use__dirname) is a node.js environment variable that point to the parent directory of the current file. – Chris Halcrow Mar 10 '21 at 02:42
  • I will just add to take a look at the cosole... maybe some "../" is missing. It works like a charm! – Daniel Azamar Jun 07 '21 at 23:00
15

Just to expand on the above, if you are using fs.readFileSync with TypeScript (and of course CommonJS) here's the syntax:

import fs from 'fs';
import path from 'path';

const logo = fs.readFileSync(path.resolve(__dirname, './assets/img/logo.svg'));

This is because fs.readFileSync() is resolved relative to the current working directory, see the Node.js File System docs for more info.

Source: Relative fs.readFileSync paths with Node.js

And of course, the CommonJS format:

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

const logo = fs.readFileSync(path.resolve(__dirname, './assets/img/logo.svg'));
Stephen Jenkins
  • 1,776
  • 3
  • 24
  • 39
  • Not sure I see how this expands the accepted answer, it seems exactly the same to me - `path.resolve` with `__dirname` and a relative path. – 404 Mar 19 '21 at 15:38
  • @404 Yes I was including for TypeScript folks :) the code is the same but using import statements. – Stephen Jenkins Mar 23 '21 at 15:23
2

Another alternative if you are using type module is to use process.cwd()

package.json

{
    "type": "module",
}

console.log(process.cwd() + relative_path) // /User/your_user/path_to_folder

Renatex
  • 957
  • 9
  • 9