I'm using this function and get this error
{ Error: EBADF: bad file descriptor, write
at Error (native) errno: -4083, code: 'EBADF', syscall: 'write' }
Function:
saveMap: function(body) {
var fd = fs.openSync("C:/mypath", 'w');
fs.writeFile(fd, body, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
fs.closeSync(fd);
},
I'm using fd so it can create the file if it doesn't exist. (I've tested with just writeFile on an existing file and it worked).
It's probably something obvious apologize for the noob question.
Related
so I was making a program that scrapes a website and stores the data in json format with fsPromises.writeFile and another program that reads the json file with fsPromises.readFile. the json file is written correctly as I've checked it manually but when I try to read it with fsPromises it returns the error mentioned below.
I've checked the file path and it's all correct, and I can literally see the json file in the directory and when I use require to parse the JSON file it works, but I want to use fs to check is the file is present and to parse from the file
I wrote the JSON file with fsPromises like this
await fsPromises.writeFile(
path.join(__dirname, "json", "productVarables.json"),
JSON.stringify(variables),
(err) => {
if (err) {
console.log("error while writing the JSON" + err);
} else {
console.log("successfuly written the JSON file");
}
}
);
and when I try to read it using fsPromises like this
await fsPromises.readFile(
path.join(__dirname, "json", "productVarables.json"),
"utf-8",
(err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
}
);
I get the following error
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
[Error: ENOENT: no such file or directory, open 'D:\desktop\Project\Node\Scraper\json\productVariables.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'D:\\desktop\\Project\\Node\\Scraper\\json\\productVariables.json'
}
fs.open('./test.txt',(err, fd) => {
fs.writeFile(fd, 'proba', (err) => {
console.log(err)
})
})
In this example when I use fd as first argument in fs.writeFile I got error:
[Error: EBADF: bad file descriptor, write] {
errno: -9,
code: 'EBADF',
syscall: 'write'
}
But if I instand of fd put path of file ('./test.txt') everything works fine.
What is happening, why I can't use file descriptor in this situation.
As I found in documentation it should be possible to use it on this way with file descriptor parameter.
When I try to run your code on node.js 14.4.0 on Windows 10, I get an EPERM error. I don't see the same error you get. I can fix that error, by passing in the open mode to fs.open() so it doesn't default to readonly. Here's the code that worked for me:
const fs = require('fs');
fs.open('test.txt', 'a+', (err, fd) => {
if (err) {
console.log(err);
} else {
fs.writeFile(fd, Buffer.from('proba'), (err) => {
if (err) console.log(err);
});
}
});
I would agree with the other commentor that it really doesn't make sense to use fs.writeFile() with fs.open() as fs.write() is more appropriate and a lot more clear what the code actually does.
Error code looks like:
{ Error: ENOENT: no such file or directory, open 'sad' errno: -2, code: 'ENOENT', syscall: 'open', path: 'sad' }
where 'sad' is the name of file I would like to write to and it doesn't exist.
Code looks like this:
fs.writeFile(filename, JSON_string, { flag: 'w' }, function(err){
if(err){
return console.error(err);
}
return JSON_string;
});
There are other similar questions, but they are all wrong in their path, starting or not starting with /, I just want to write a file on root from where I run this node.js application (it is also initialized with npm in this directory..).
Running with
sudo node server4.js
Doesnt work either.
Changing flags to w+ or wx or whatever, doesn't help.
Code works if file exists.
Node v9+.
I need to use writeFile() function.
This is working for me, please check if this works in your system:
var fs = require('fs')
fs.writeFile('./myfile.txt', 'Content to write', { flag: 'w' }, function(err) {
if (err)
return console.error(err);
fs.readFile('./myfile.txt', 'utf-8', function (err, data) {
if (err)
return console.error(err);
console.log(data);
});
});
(besides writing it also reads to confirm)
I'm trying to make a download link on my server for a zip file and I'm currently getting this error: (Note, still just testing it on local machine)
{ [Error: ENOENT: no such file or directory, stat 'C:\Users\Jordan\Desktop\Websites\HappyCamel\Users\Jordan\Desktop\Websites\HappyCamel']
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path: 'C:\\Users\\Jordan\\Desktop\\Websites\\HappyCamel\\Users\\Jordan\\Desktop\\Websites\\HappyCamel',
expose: false,
statusCode: 404,
status: 404 }
The relevant code is this:
router.get('/file/:name', function(req, res, next) {
console.log('test123'); //successfully prints to console
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
console.log('test456'); //successfully prints to console
if(err) {
console.log(err) //I'm assuming this is source of logged error
} else {
console.log("no error"); //doesn't print
}
});
})
edit:
Fixed it with changing this line:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
to
res.download('./test123.zip', 'test123.zip', function(err) {
but now I get
angular.min.js:114 ReferenceError: success is not defined
error on my browser, but no errors in my node console (my "no error" line is printing)
you are using relative path. when you do this:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
it will look for Users/Jordan/Desktop/Websites/HappyCamel/ inside your current file's directory. looks like what you need is full path, or better a correct relative path- from the error it looks like the file is located with your code, so this should do:
res.download('./', 'test123.zip', function(err) {
I'm trying to use utorrent-api library for node.js as in example:
request({'uri' : 'http://releases.ubuntu.com/13.04/ubuntu-13.04-desktop-i386.iso.torrent', 'encoding': null}, function (error, response, torrentFileBuffer) {
utorrent.call('add-file', {'torrent_file': torrentFileBuffer}, function(err, data) {
if(err) { console.log('error : '); console.log(err); return; }
console.log('Successfully added torrent file !');
console.log(data);
});
});
and I'm getting this error in console:
error :
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
I can connect to uTorrent with:
utorrent.call('list', function(err, torrents_list) {
console.log(torrents_list);
});
And I'm getting torrents list correctly.
I tried to save .torrent file to disk and it looks ok, so the problem is with file upload.
uTorrent 3.3 is running on Linux. WebUI is working and I can upload .torrent files through browser.
How can I debug this error?