I'm trying to delete a folder recursively but the folder itself is still there (empty) when using fs.rmdir(dest, { recursive: true });
Has anyone else come across this issue and if so how did you manage to fix it?
I'm using Node v14.17.0
A hacky solution to anyone else having the same issue:
fs.rmdir(dest, { recursive: true }, (err) => {
if (err) throw err;
try {
if (fs.existsSync(dest)) fs.unlinkSync(dest);
} catch (e) {
// handle error
return;
}
});
Related
I'm trying to make a function that checks if file is opened or in use by another app or process using fs
and here is my attempt to do this
function isFileInUse() {
try {
const file = path.join(__dirname, '20200201072955946-2.jpg');
//try to open file for checking if it is in use
fs.access(file, fs.constants.W_OK, (err) => {
if (err) {
console.log('File is in use');
} else {
console.log('File is not in use');
}
});
return false;
}
catch(error) {
console.log(error);
return true;
}
}
isFileInUse()
this code somehow move the the image with no error even if it's run by another app like notepad++ or by image viewer
if someone can tell me what i'm doing wrong on this matter or help with this . that be great
thanks in advance
I keep getting an error: TypeError: Cannot read property 'doors' of null. I want to be able to set doors to be nullable, as to be able to avoid this error and simply hit the error response and return 404. However, I am not sure how to do this?
Here is my code:
Data.findOne({
'_id':'6182544c20d538aefe49def0',
'doors.id':doorId
}, {
'doors.$':1
}, function(err, data) {
if (err) {
res.status(404).send('No Matching Door Found')
} else if (data.doors[0].status === 'open') {
res.status(401).send('Door already unlocked')
} else {
res.status(200)
}
})
The error is hit on the third line, where It cannot find a doors object where the ID is equal to doorId.
I have tried setting doors.id to !doors.id, however, this then kept hitting the 404 regardless of what was being entered.
Any help is appreciated, thank you.
Well it seems that your entire query is wrong.
I would suggest first to try this query for your needs:
Data.findOne({
'_id':'6182544c20d538aefe49def0',
'doors.id':doorId
}, {
'doors.$':1
}).exec(function(err, data) {
if (err) {
res.status(404).send('No Matching Door Found')
} else if (data.doors[0].status === 'open') {
res.status(401).send('Door already unlocked')
} else {
res.status(200)
}
})
Then I recommend you to read more about MongoDB and Mongoose.
What I need:
Create a folder on the desktop, within that folder create another called "img" and inside img create "home".
The way I managed to do it, But I know it's not the ideal way ... I'm still learning, thank you for your patience!
Any suggestions to improve this?
var nome = 'teste';
const dir = `C:/Users/mathe/Desktop/${nome}`;
if (!fs.existsSync(dir)){
fs.mkdir(dir, (err) => {
if(err){
console.log(err)
}else{
dirImg = dir+'/'+'img';
fs.mkdirSync(dirImg)
fs.mkdirSync(dirImg+'/'+'home')
console.log('Sucess')
}
});
}else{
console.log(`File $ {name} cannot be created because it already exists!`)
}
mkdir has a recursive option, so:
if (!fs.existsSync(dir)){
fs.mkdir(`${dir}/img/home`, {recursive: true}, (err) => {
// ^^^^^^^^^^^^^^^^^−−^^^^^^^^^^^^^^^^^^−−−−−−−−−−−−−−−−−−−
if(err){
console.log(err)
}else{
console.log('Sucess')
}
});
}else{
console.log(`File $ {name} cannot be created because it already exists!`)
}
Side note: It's generally not a good idea to check for existence before creating. Instead, just go ahead and try to create it. From the documentation:
Calling fs.mkdir() when path is a directory that exists results in an error only when recursive is false.
So:
fs.mkdir(`${dir}/img/home`, {recursive: true}, (err) => {
// ^^^^^^^^^^^^^^^^^−−^^^^^^^^^^^^^^^^^^−−−−−−−−−−−−−−−−−−−
if(err){
console.log(err)
}else{
console.log('Sucess')
}
});
When I am trying to load the following package in Meteor https://github.com/vsivsi/meteor-job-collection
It gets downloaded 100% and extracted, but at the time of loading it throws the following error:
{ [
Error: ENOTEMPTY: directory not empty, rmdir 'C:\Users\LALITS~1\AppData\Local\Temp\mt-16riklk\npm\job\node_modules']
errno: -4051,
code: 'ENOTEMPTY',
syscall: 'rmdir',
path: 'C:\\Users\\LALITS~1\\AppData\\Local\\Temp\\mt-16riklk\\npm\\job\\node_modules' }
I am using windows 8.1 64 bit.
I have tried to delete the folder manually, but again it created a new one and throws the same error. Can anyone tell me what is the problem? Am I missing something?
Thanks in advance.
Your issue looks like this known Meteor bug:
https://github.com/meteor/meteor/issues/8663. This bug occurs under Windows when updating to the next Meteor version.
Maybe you can try the proposed solution, which is to edit the following file:
C:\Users\[yourName]\AppData\Local\.meteor\packages\meteor-tool\[yourMeteorVersion]\mt-os.windows.x86_32\tools\fs\files.js
...and replace functions files.rm_recursive_async and files.rm_recursive with this code:
files.rm_recursive_async = function (path) {
return new Promise(function (resolve, reject) {
rimraf(files.convertToOSPath(path), function (err) {
err && console.log(err);
resolve();
//return err ? reject(err) : resolve();
});
});
}; // Like rm -r.
files.rm_recursive = Profile("files.rm_recursive", function (path) {
try {
rimraf.sync(files.convertToOSPath(path));
} catch (e) {
if (e.code === "ENOTEMPTY" && canYield()) {
files.rm_recursive_async(path).await();
return;
}
console.log(e);
//throw e;
}
}); // Makes all files in a tree read-only.
I use node glob which is working OK .
I use it for one folder1 like following
glob('folder1/*.js'), function(err, files){
if (err) {
console.log('Not able to get files from folder: ', err);
} else {
files.forEach(function (file) {
https://github.com/isaacs/node-glob
Now I want to read in one shot from folder2 also and I try to use globby like following and I got error
globby(['folder1/*.js','folder2/*.js']).then( function(err, files){
if (err) {
console.log('Not able to get files from folder: ', err);
} else {
//Get plugin configuration & provided actions
files.forEach(function (file) {
https://github.com/sindresorhus/globby
in this case the files are coming as undfiend and I got to the error any idea why
Try to remove err argument from the then callback. Use catch to handle errors
globby(['folder1/*.js','folder2/*.js']).then( function(files){...}).catch(function(err){...})