Facing issue in using mv() module in node - javascript

encountering with error ENOENT : rename filename,
am unable to understand this error why am facing can anyone help me out.
below is my code
mv(query.sourceLocation, query.targetLocation, {
mkdirp: true
}, function (err) {
if (err) return console.error(err+new Date());
res.end("moved");
});

Related

Error: ./node_modules/zlib/lib/zlib.js './zlib_bindings'

i finished upgrading to angular 11 and i have error in zlib library. I need this to convert a Ibuffer. When i uploading the project I get this error:
Error: ./node_modules/zlib/lib/zlib.js
Module not found: Error: Can't resolve './zlib_bindings' in '..\node_modules\zlib\lib'
The code:
const zlib = require('zlib');
const buffer = Buffer.from(data,'base64');
zlib.inflateSync(buffer, (err,buffer) => {
if (err) {
console.error('An error occurred:', err);
}
this.excelService.export("xxx" + `${base.name}`, buffer.toString());
});
I had similar problem but when trying to add firebase Admin SDK and thanks to some guy on github i just make a copy and then renamed the copy zlib/lib/zlib - Copy.js to zlib/lib/zlib_bindings.js and then it started working.
Link to github issue:
https://github.com/diegomura/react-pdf/issues/1678

nodejs not creating the file that specified using fs.open and throwing error 4058-ENOENT

I am using this block of code to create and write a new directory and a file.
I just started to learn nodejs
var lib = {};
lib.baseDir = path.join(__dirname,'/../.data/');
lib.create = function(dir,file,data,callback){
fs.open(lib.baseDir+dir+'/'+file+'.json', 'wx', function(err, fileDescriptor){
if(!err && fileDescriptor){
var stringData = JSON.stringify(data);
// Write to file and close it
fs.writeFile(fileDescriptor, stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
} else {
callback('Error closing new file');
}
});
} else {
callback('Error writing to new file'+'lib.baseDir');
}
});
} else {
callback(err);
}
});
};
but I am repeatedly getting the error
{ Error: ENOENT: no such file or directory, open 'C:\Users\Jawahr\Documents\GitHub\node-api\.data\test\newFile.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Jawahr\\Documents\\GitHub\\node-
api\\.data\\test\\newFile.json' }
calling this library in a index.js as
var _data = require('./lib/data');
_data.create('test','newFile', {"asdksadlk" : "asldj"} ,function(err) {
console.log('this was the error ',err);
});
I've been stuck here for a while, is it because the pathname and filename contained the part "C:" which has colon a reserved char in windows 10, if it is the problem how to solve this.
using windows 10 and NodeJs 8.6.
Can you try this -
fs.open(lib.baseDir+dir+'/'+file+'.json', 'w', function(err, fileDescriptor){
It looks like 'wx' throws an error if file exists -
'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
'wx' - Like 'w' but fails if the path exists.
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
'wx+' - Like 'w+' but fails if the path exists.
Referred from here
Looks like your put non-existing or non-accessible path to your file. Look:
fs.open('/path/is/not/exists/xx.js','wx',(err,fd)=>{
if (err) {
console.log(err.message);
}
});
and got
Error: ENOENT: no such file or directory, open '/path/is/not/exists/xx.js'
In case when file is already exists you will got something like Error: EEXIST: file already exists, open '...'
And last, but not least. Instead of lib.baseDir+dir+'/'+file+'.json' better solution is use path.join(lib.baseDir,dir,file+'.json') from path module
Add a check for directory or create before fs.open
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
Then the rest of your code will work. as fs.open only creates file if it doesn't exist, it does not create directory

How to display a popup Window just when a browser is started running from nodejs

I want to popup a window on Internet explorer windows 10 from nodejs.
I did research on google but I found only the window-popup, popup packages but it is throwing me error something like this ReferenceError: window is not defined
at windowPopup
Even after installing the packages thrpugh npm.
I am doing this on WebStorm ide.
exec(`START iexplore.exe`, (err, stdout, stderr) => {
if (err) {
throw err
}
console.log('stdout', stdout)
console.log('stderr', err)
});
var popup = require('window-popup').windowPopup;
What is the correct way to do this, please help me out where I am going wrong.
Thanks.

delete folder containing files node js

I am looking for a way to delete folders that contain files in node.js ?
I know there exists a way to delete empty folders using fs.rmdir(), and i tried using the npm rimraf module that provides the function "rm -rf" for node.js
socket.on("end", function (data) {
rimraf("./a/b/c", function(err){
if(err){
console.log(err);
}
});
});
but i keep getting this error.
{ [Error: ENOTEMPTY: directory not empty, rmdir './a/b/c']
errno: -39,
code: 'ENOTEMPTY',
syscall: 'rmdir',
path: './a/b/c' }
So I tried another way around this issue, first i empty the directory then i delete the directory
socket.on("end", function (data) {
rimraf("./a/b/c/*", function(err){
if(err){
console.log(err);
}else{
fs.rmdir("./a/b/c")
}
});
});
but then i get this error
Error: ENOTEMPTY: directory not empty, rmdir './a/b/c'
at Error (native)
I checked the folders the rimraf deletes the files but i don't see why i am still getting an error with fs.rmdir().
Edit :
I looked up a different module called fs-extra and came up with this.
fse.emptyDir("a/b/c/", function(err){
if(err){
console.log(err);
} else {
console.log("doneaaaa")
fse.remove("a/b/c",function(err){
if(err){
console.log(err);
} else {
console.log('doneaswell');
}
});
}
});
Now i get this error :
doneaaaa
{ [Error: EBUSY: resource busy or locked, unlink 'a/b/c/.nfs000000002ab5000d00000072']
errno: -16,
code: 'EBUSY',
syscall: 'unlink',
path: 'a/b/c/.nfs000000002ab5000d00000072' }
As you can see i get the past the first part of the function that deletes the files from the folder but when it comes to deleting the folder it throws the EBUSY error.
Thank you in advance !
Regardin the EBUSY error, do one thing. Do console.log(process.cwd()) to see what directory Node process is in before it tries to delete the folder. If Node is in the same folder it is trying to delete, then it will issue the EBUSY error. I had that happen to me in a Node.js application I'm developing. The solution was to change directory (process.chdir(new directory)) to something other than the one I'm trying to delete before I attempt to delete it, and problem solved. This happened on Windows by the way.
To remove it syncronously:
var fs = require('fs');
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};

Error: ESPIPE, write

Im using the following code to write to a file
fs.writeFile('/dev/ttymxc1',"LED 1 ON",function(err){
if (err) return console.log(err);
});
and I get the error
{ [Error: ESPIPE, write] errno: -29, code: 'ESPIPE' }
I already googled it but I found nothing..
EDIT:
I think the reason this does not work it that I have to use
fs.createWriteStream();
instead of
fs.fileWrite();
The Solution was using
fs.createWriteStream()
instead of
fs.fileWrite();

Categories

Resources