How to use path module from Node.js to solve file not found issue.

Aswanth KP
4 min readFeb 20, 2021

So if you are working with Node.js you probably faced an issue where you needed to read a file but you ended up a file not found error because of the invalid pathname.

if you faced then you know, but if you don’t let me explain,

file structure

so here we can see the directory structure, data folder contain two csv file which we needed to read, for that we have a function in dataread.js under src/server using this function we will read the csv and convert it to json object.

above you can see the function getDataObject to read and convert csv to json. Here we are using csvtojson npm package, feel free to check it out. We are passing the file name as a parameter to the function and we are concatenating that to create a relative path to the somefile.csv and passing that to the csv function.

here we will test that function in the test/test.js, and if run

here we can see it worked, hureyyyyy..

but wait what if we changed the position of the test file, hmm let’s put our test file in the src folder and see what will happen.

As you can see that the test.js is now in src/text, but look we got an error, why? do you have any idea, OK let me explain the file path is given relative to the test.js so when we move the file it is no more a valid relative path between test.js and somefile.csv so it threw an error, simple.

Now you want the solution right, wait, before going to explain the solution I want you to introduce to a module called path, path is node module which makes our life easier to work with directories and files.

const path = require('path');

there are many useful functions in path module, but we are interested only in 2 functions

path.join([...paths]path.relative(from, to)

for more information, you can check out the official documentation, but I will give you a basic idea of how these work.

path.join() is used to join two or more paths given as arguments and create one single path.

path.relative() is used to find relative path between two given paths, it will read from left to right and create a relative path based on the current working directory.

Our goal is whatever the current working directory (which will be the directory of the file we are executing) file path should not depend on it, if simply put there should not be any error regarding the file path irrespective of the current working directory.

So let us take a moment and think about how to solve it. First, we need to find the path from the current working directory to the somefile.csv file. For that we can use path.relative(from,to). Parameters from will be the current working directory (path of the folder where the current JavaScript file resides here it is dataread.js) and to will be the absolute path of somefile.csv file.

So for the path.relative(from, to) we need 2 path as arguments,

1. Current working directory, for that we will use a function from process module called process.cwd().

2. Absolute path of our csv file that we need to read. For that we need to use Path.Join()

Path.join() will give us the absolute path to the csv file, for that, we need to give 2 arguments,

1. Path to the dataread.js where we are reading the csv in getDataObject(). For that we use a special node.js specific variable called __dirname which will give us the path of current JavaScript file.

xxxxx@xxxx:~/Project_3/src/test$ node test.js 
/home/aswanth/Moutblue/Project_3/src/server

here u can this is what we will get when we print the __dirname.

2. The relative path from current file(dataread.js) to the target folder where our csv is located .

Which is '../data'

And if we pass these two as arguments to the path.join we will get

const absolute = path.join(__dirname,'../data')>> /home/aswanth/Moutblue/Project_3/src/data

and we got our absolute path of the folder containing csv files.

next, we will give this path as an argument to the path.relative() along with the current working directory

const relative =path.relative(process.cwd(), absolute)>> ../data

Voilaaa, we got our relative path

here is the updated getDataObject().

Now we will see what will be the output of that console.log for our two different test.js files which reside in test/ and src/test.

Above we can see the output of each console.log when we execute the test file from /test folder

Here is the output when we execute test in src/test

We can see the difference in the relative path between test.js and somefile.csv. In both the case, our data parsed successfully without any error.

It is time to say goodbye, hope this article helped you to understand how to use path.join and path.relative together to read files effectively.

Thank You. Adios

--

--