node.jspathbackendscandirenoent

Node.js name too long, scandir


I have a folder structure which starts at my project Note that user.hash and user are MD5 hash

root/data/${user.hash}/

Now, what i need to do is read the files in side that directory using:

  var companies = fs.readdirSync(`../data/${user}/`);

I also tried

var BASE_FOLDER = path.resolve(__dirname, "..");

  var companies = fs.readdirSync(`${BASE_FOLDER}/data/${user}/`);

And in both cases i get the following error:

UnhandledPromiseRejectionWarning: Error: ENAMETOOLONG: name too long, scandir '../data/callback => {
                AND HERE MY CODE FOLLOWS

So far what i understood is that the file path string is too long ? How can we workaround an error like this if we are limited to that certain path... ?


Solution

  • It looks like user is a function for some reason (depends on where it comes from / where you initialize it) and what you see in the error message ../data/callback => { ... is the stringified version of that function.

    I would double check that user is really just a string identifier for the user. Based on your first example, shouldn't you use user.hash?