0

I have the code below in a file named file-create-directory.js when I call this file on the terminal by node file-create-directory.js is giving me an error I see it has something to do with the fs.exists module but I can't figure it out

fs.js:140 throw new ERR_INVALID_CALLBACK(); TypeError [ERR_INVALID_CALLBACK]: Callback must be a function at maybeCallback (fs.js:140:9) at Object.fs.exists (fs.js:218:3)

const fs = require('fs');



if(!fs.exists("views")) {

fs.mkdir("views", (err)=>{

   if(err) return err;

    fs.writeFile("./views/new.html", 'this is a new directory and data', (err)=>{

        if(err) return err;

       console.log('Directory and file saved')

    })


});

}
3
  • 1
    fs.exists is deprecated so use this method instead: nodejs.org/api/fs.html#fs_fs_existssync_path Commented Oct 22, 2018 at 14:48
  • I see fs.existsSync works , I don't need asynchronous, this helped me, but what if I needed to check this without blocking, is there a different function I can use? Commented Oct 22, 2018 at 14:55
  • 1
    You can use fs.access(). Commented Oct 23, 2018 at 13:04

1 Answer 1

1

You are using the "exists" function of built-in "fs" module, which should get a callback. If you would like to perform it synchronously without a callback, you should use "existsSync".

Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.