Can fs.unlink() delete a empty or non empty folder? - javascript

I am new to Node.js.
const fs = require('fs');
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
This is some code that I copied from a node.js document file system intro example.
But, I am confused. Can unlink() delete a folder or not?
I have tried but it doesn't work.
So, can unlink() delete a folder or not?

The fs.unlink(path, callback) function is used to delete a file not a folder.
To remove a folder you can use the fs.rmdir(path, callback) function instead.

Related

How to find tmp folder made by nodejs filesystem?

I was using fs to write values from textarea into a file and had issues with the path until I found on stackoverflow that the path has to be a tmp folder. I did that and the terminal shows the function was successful but now I don't know where that folder is and how I can access.
app.post('/scheduleTweet', function (req, res) {
fs.writeFile('/tmp/scheduleTweet', req.body.text, err => {
if (err) {
console.error(err);
} else{
console.log("success");
}
});
})
Can somebody help me please? I'm self taught so not really sure what to google even.
If the path starts with a /, then fs.writeFile writes to that path on the current drive, for example, to C:/tmp/schedule.txt. Otherwise, it writes relative to the current working directory of the node process. It's easier if you make it relative to the directory containing your Javascript code: fs.writeFile(__dirname + "/tmp/schedule.txt", ...).

sqlite3 sql query functions in specific folder not working when used in other folders of my express API project

SQLite Queries in one folder, which has links to the dataBase. When importing these Functions to locations to use they are trying to apply them to db in the current repository which the function is being run.
I have previously stated where the db is using in the file with the Queries, however, this route is not seeming to be used.
In the first folder i have a file which is trying to run the imported SQL query function...
In a folder inside the above folder I have a file (sql.js) where i am exporting the SQL query functions, and the Database.sqlite is in this folder.
// this is my exported function with route to data.sqlite
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./data.sqlite');
const getData = (data) => {
db.all(`SELECT title, desc, details FROM "${data}"`, (err, rows) => {
if (err){
console.log(err);
} else {
console.log(rows);
}
});
}
// this is the file importing and trying to run that function:
getData('foo');
/*
The error code:
[Error: SQLITE_ERROR: no such table: foo] {
errno: 1,
code: 'SQLITE_ERROR'
}
It also creates a new table in the same directory as the importing file
*/
I have worked out what was wrong, the route to the database is from the root directory, not from the current directory that the function is in!
HOPE this helps anyone having a similar issue. :)

Unlink files inside fs.watch in node

Is there a way or package that allows me to watch for new files and delete them?
Even if i try to delete files in sub directories. i get errors because the file is locked.
fs.watch(example, function (eventType, filename) {
fs.unlink("example/"+filename+"/testfile", (err) => {
if (err) throw err;
console.log('successfully deleted');
});
});
Appreciate your time

fs.unlink() does not work and callback does not fire

It was working a while ago, and I'm not sure what changed to where the fs.unlink() function seems to be totally skipped. The file for deletion is saved in the root package directory, while the typescript file that contains my code is in the dist folder. It worked when I passed in only the fileName like this: fs.unlink(fileName). But now that doesn't work, and it also doesn't it work when I explicitly direct it to the root folder by using either fs.unlink("./" + fileName) or fs.unlink("../" + fileName), or even when I hardcode the path!
Below is my code:
s3.putObject(params, function(err, data){
// ...
console.log("Data uploaded to S3 bucket");
fs.unlink("./" + fileName, function(err){
if(err) {
return console.log("Delete error: " + err);
}
else{
console.log("file deleted successfully");
}
});
console.log("Before process.exit()");
process.exit();
});
When run, this code logs:
Data uploaded to S3 bucket
Before process.exit()
So as you can see, it skips both the if and else statements on fs.unlink(), and doesn't delete the file. It's acting like that block of code doesn't exist at all and I can't figure out why.
You are using async unlink function that's why it is executing next statement. Use
unlinkSync instead.

Is there a method for clearing out the contents in a csv file before appending data?

I want to overwrite or clear out the contents in a csv file before I append new data. Is there a method for this ?
Just write to the file using fs.writeFile, it will override an existing file (if one exists).
const fs = require('fs');
fs.writeFile('message.csv', 'new content', (err) => {
if (err) throw err;
console.log('The file has been saved!');
});

Categories

Resources