55

I'm currently starting up my NodeJS application and I have the following if-statement:

Error: ENOENT, no such file or directory './realworks/objects/'
    at Object.fs.mkdirSync (fs.js:654:18)
    at Object.module.exports.StartScript (/home/nodeusr/huizenier.nl/realworks.js:294:7)

The weird thing, however, is that the folder exists already, but the check fails on the following snippet:

if(fs.existsSync(objectPath)) {
    var existingObjects = fs.readdirSync(objectPath);
    existingObjects.forEach(function (objectFile) {
        var object = JSON.parse(fs.readFileSync(objectPath+objectFile));
        actualObjects[object.ObjectCode] = object;
    });
}else{
    fs.mkdirSync(objectPath); // << this is line 294
}

I fail to understand how a no such file or directory can occur on CREATING a directory.

Ruben Rutten
  • 1,659
  • 2
  • 15
  • 30
  • let me see you objectPath, pls – siavolt Feb 13 '15 at 11:21
  • Object path is what the error returns, `./realworks/objects/` – Ruben Rutten Feb 13 '15 at 11:21
  • 5
    Are you trying to create those directories recursively? fs.mkdir() doest or can not do that. My guess is that folder "realworks" doesnt exist yet, thats why its throwing. If its the case, try using: https://www.npmjs.com/package/mkdirp – David Losert Feb 13 '15 at 11:22
  • @Charminbear Both directories (parent and child) are in fact already existant. So the first problem is that it seems to fail the fs.existsSync, then it fails to create a directory that already exists. – Ruben Rutten Feb 13 '15 at 11:24
  • The `./realworks/objects/` is relative to the node executable and not to your script, just saying. – Aleksandr M Feb 13 '15 at 12:01
  • Let me try something else – Ruben Rutten Feb 13 '15 at 12:02
  • I was thinking starting the process through `forever` wouldn't make my application behave different if I opened it from the directory above `app.js`, so, `mywebsite/app.js` through forever is different from `cd`ing to the folder and then running forever with `app.js` – Ruben Rutten Feb 13 '15 at 12:16

10 Answers10

76

When any folder along the given path is missing, mkdir will throw an ENOENT.

There are 2 possible solutions (without using 3rd party packages):

  • Recursively call fs.mkdir for every non-existent directory along the path.
  • Use the recursive option, introduced in v10.12:
    fs.mkdir('./path/to/dir', {recursive: true}, err => {})
Yoav Kadosh
  • 4,807
  • 4
  • 39
  • 56
  • How can I know 'recursive option is introduced in v10.12'? any official documents? – QuinnChen Feb 28 '19 at 13:39
  • 1
    @ZilMike yes, the official docs have a history section for every fuction (you need to expand it by clicking on it) See https://nodejs.org/api/fs.html#fs_fs_mkdir_path_options_callback – Yoav Kadosh Mar 03 '19 at 11:26
  • Weirdly, in 10.19.0, its not working, even with the flag. – Rob Dec 05 '21 at 15:54
24

Solve here How to create full path with node's fs.mkdirSync?

NodeJS version 10.12.0 has added a native support for both mkdir and mkdirSync to create a directory recursively with recursive: true option as the following:

fs.mkdirSync(targetDir, { recursive: true });

And if you prefer fs Promises API, you can write

fs.promises.mkdir(targetDir, { recursive: true });
ztvmark
  • 1,319
  • 15
  • 11
9

When you are using fs.mkdir or fs.mkdirSync, while passing the path like folder1/folder2/folder3, folder1 and folder2 must exist otherwise you will get the above error.

Abhishek Kumar
  • 820
  • 10
  • 18
3

The following worked for me:

fs.mkdir( __dirname + '/realworks/', err => {})
standac
  • 1,027
  • 1
  • 11
  • 26
0

Problem was caused by forever running the application relative to the working directory the forever start command is called in, not the location of the application entrypoint.

Ruben Rutten
  • 1,659
  • 2
  • 15
  • 30
  • 8
    So what is the solution ?? You only tell us the cause from your point of view. You should rather give a solution that which you can accept – olyjosh May 30 '17 at 22:25
0

Try:

fs.mkdir('./realworks/', err => {})
Pang
  • 9,564
  • 146
  • 81
  • 122
0

The reason for the error is that if any of the folders exist along the path given to fs.mkdir or fs.mkdirSync these methods will throw/callback with an ENOENT error.

wibobm
  • 696
  • 7
  • 13
0

ENOENT is described in the linux documentation as the following:

No such file or directory (POSIX.1-2001).

Typically, this error results when a specified path‐ name does not exist, or one of the components in the directory prefix of a pathname does not exist, or the specified pathname is a dangling symbolic link.

Another possible reason for ENOENT is that you lack sufficient privileges to create the directory.

This happened to me while building a docker image where I didn't have sufficient privilege to create a subfolder in the current WORKDIR. Changing the owner of the folder using --chown=user:usergroup OR changing the USER to the root user for the directive were both valid solutions to the problem.

Philippe Hebert
  • 1,616
  • 2
  • 24
  • 51
0

WHAT WORKED FOR ME WAS ;

  1. Deleting my yarn.lock, package-lock.json, and nodemodules
  2. reinstalling with yarn build
  3. restarting my local server
-1

so... you probably might be using ubuntu terminal to create your react app.

It happens to me that I am testing the ubuntu terminal that windows recently launched to be installed on windows computer, like a virtual machine but actually not so messy.

I occured to have the same error as you folk, after testing all the options that the community has given before, none of them work. However, i did find a solution for my problem. It was giving me the ENOENT error, like, test file or directory not found. but I was there indeed. I was using npm start, and came up with the idea of using sudo npm start... and it worked.