0

I'm new to NodeJS and this I can't get this code to work. I want to delete the file inside stuff first, then delete the file itself.

What am I doing wrong please?

     var fs = require("fs");
     fs.unlink('./stuff/writeMe.txt', () => {
     fs.rmdir('stuff');
     });
1
  • 1
    you need to add a callback function to your rmdir Commented Mar 19, 2020 at 7:13

3 Answers 3

2

Calling asynchronous functions like fs.unlink and fs.rmdir in Node starts a task. You pass them a function as an argument – a callback – that gets called when that task completes. The callback has an argument that tells you whether the operation completed successfully.

You’re already making use of this partially by only calling fs.rmdir when fs.unlink has completed. Now you need to say what to do when fs.unlink has completed (even if it’s nothing), and start checking for errors.

An example of something you can do when there’s an error is throw that error, which prints it and exits the process.

fs.unlink('stuff/writeMe.txt', (err) => {
    if (err) throw err;

    fs.rmdir('stuff', (err) => {
        if (err) throw err;
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Never, ever recommend throw err inside a plain asynchronous callback. It does NOTHING useful. Show an example of real error handling. I know the node.js doc shows it this way and it's teaching a whole generation of newbies the wrong way to write error handling. A throw into a plain asynchronous callback goes nowhere useful where you can catch it (it goes back into the asynchronous innards of the fs library in this case).
@jfriend00: It doesn’t do nothing. It “prints [the error] and exits the process”, like I wrote in the answer. For scripts, that’s what you want a lot of the time. (Well, that with promises, but it’s a similar .catch(err => { process.nextTick(() => { throw err; }); }).)
It's only in not very serious programs that you want your program to just abnormally exit when there's an error. 99.99% of the time you're writing code that some caller will call and the caller wants to know if there was an error. Not the best example to teach a newbie how to code. I'm not sure why you're getting defensive here when you know this is not what you should be teaching a newbie and you could demonstrate a better practice. My point of commenting was so that the OP knows they should usually not write this type of code.
1

I would recommend using the new promises interface for the fs module withg async/await as it makes it easier to sequence asynchronous operations without nesting. It also makes error handling much simpler:

const fsp = require("fs").promises;

async function someFunction() {
     try {
         await fsp.unlink('./stuff/writeMe.txt');
         await fsp.rmdir('./stuff');
     } catch(e) {
         // got an error, handle it here
         console.log(e);
     }
}

4 Comments

How do I call the promise interface without awaiting?
@AntonDuzenko - I don't know what you mean. All async functions return a promise so the caller has to use either await or .then() to know when they are done or retrieve a completion value.
I don't need to know when done or retrieve a completion status for unlink of a temp file. But I do get this error when calling the async version without the callback param.
@AntonDuzenko - Yep, you're not allowed to call regular fs (non-promise) functions without a callback. You can make it a do nothing callback () => {} if you really want to ignore errors, but you have to pass the callback.
0

You can use rimraf package (recursive delete). Otherwise is this the only file in directory?

2 Comments

How does this relates to the question?
The code in the question removes the file and then the directory where the file was. rmdir works for empty directory, so my angle at this was that there could be other files in dir or you cold just use rimraf to remove whole directory with files inside.

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.