I'm getting "Error: EISDIR: illegal operation on a directory, read" after trying to read the content of a .json. This is how I'm trying to access the file. I'm using the FileSystem of node js.
fs.readFile( path, ( err, fileData) => {
if (err) {
throw err;
}
else {
return fileData;
}
});
While debugging I can see that the error is thrown before the if statement.
Any idea?
Maybe the path to the file is not the right one, make sure the path of your file looks like the one that appears in the following code:
const fs = require('fs');
fs.readFile('PATH_TO_YOUR_FILE/File_Name.json', (err, fileData) => {
if (err) {
throw err;
} else {
console.log(JSON.parse(fileData));
}
});
Related
I'm currently stuck trying to retrieve a file from file system in order to send it through api to the client. For my backend I'm using express js
I'm using fs library and currently I'm trying to do it with readFile function, but I want to do it without specifying the file name or just the file extension because it will depend from file file will be uploaded from client.
What I tried until now (unsuccessfully) is shown below:
router.get("/info/pic", async (req, res) => {
const file = await fs.readFile("./images/profile/me.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/me.*'
return;
}
console.log(data);
});
});
const file = await fs.readFile("./images/profile/*.*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*.*'
return;
}
console.log(data);
});
const file = await fs.readFile("./images/profile/*", (err, data) => {
if (err) {
console.log(err); // Error: ENOENT: no such file or directory, open './images/profile/*'
return;
}
console.log(data);
});
If I specify the file name everything works fine, like: fs.readFile("./images/profile/me.jpg". but as I said, I don't know for sure the right extension of that file.
Important info: In that directory there will be only one file!
Please help me!
Thank you in advance!
If there is only one file in the directory, the following loop will have only one iteration:
for await (const file of fs.opendirSync("./images/profile")) {
var image = fs.readFileSync("./images/profile/" + file.name);
...
}
const fs = require('fs');
fs.readdir('./images/profile', function (err, files) {
//handling error
if (err) {
return console.log('err);
}
files.forEach(function (file) {
// Do whatever you want to do with the file
});
});
I'm trying to copy a file with the data inside a new file in a folder.
I tried doing this but it didn't work:
copyFile(`./data/guilddata/guilds/default/GUILDID.json`, `./data/guilddata/guilds/${guild.id}/GUILDID.json`, (err) => {
if (err) throw err;
});
https://www.npmjs.com/package/fs-copy-file
Does anyone know what to do? (${guild.id} just means the guild id, the folder is already there). I also get no errors. Thank you
const fs = require('fs');
fs.copyFile('./data/guilddata/guilds/default/GUILDID.json', './data/guilddata/guilds/' + guild.id + '/GUILDID.json', (err) => {
if (err) throw err;
console.log('All done! The file is copied!');
});
Is it possible to read from /dev/pts/1 with nodeJS?
If i cat /dev/pts/1 i can see data.
I tryed to use this :
var fs = require('fs');
fs.readFile('/dev/pts/1', function(err, data) {
if (err) throw err;
console.log(data);
});
This give no output.
And also no error.
Image of running the script with no output
Here is a code:
'use strict';
let fs = require('fs');
module.exports = (fileName, res) => {
fs.createReadStream(fileName)
.pipe(res)
.on('error', err => {
console.log('==========>> ERROR : ' + err.message);
res.statusCode = 500;
res.end('Internal Server Error: 500');
})
.on('end', res.end);
};
When specify wrong "fileName" then server crashes with "ENOENT: no such file or directory" message.
Why doesn't this code catch the error in the .on('error', ...) block?
Thanks!
At first glance, I think you aren't actually attaching it to read stream (as I figure out you wanted).
See Stream.pipe() documentation:
The readable.pipe() method returns a reference to the destination
stream making it possible to set up chains of piped streams
There isn't any error over destination stream, so error callback isn't called.
Try:
module.exports = (fileName, res) => {
fs.createReadStream(fileName)
.on('error', err => {
console.log('==========>> ERROR : ' + err.message);
res.statusCode = 500;
res.end('Internal Server Error: 500');
})
.on('end', res.end)
.pipe(res);
};
...or (clearer and more undestandable):
module.exports = (fileName, res) => {
var iStream = fs.createReadStream(fileName);
iStream.on('error', err => {
console.log('==========>> ERROR : ' + err.message);
res.statusCode = 500;
res.end('Internal Server Error: 500');
});
iStream.on('end', res.end);
var oStream = iStream.pipe(res) // returns refernce to the output stream (actually res);
};
...on the other hand, I'm not sure if stream will actually be created if file doesn't exists.
Probably fs.createReadStream() itself would throw an error before actually creating the stream so, for this reason, you should probably enclose it in a try {...} catch(){...} block.
Anyway I'm not sure about that. It would be more handy (from the coding point of view) if createReadStream() creates the stream before opening the file and then throws the error, (over the stream) if the file actually doesn't exists. But this should be investigated or tested before.
I am trying to connect to mongoDB from node.js and upload a file("functions") to MongoDB.
Can someone please verify whats the issue with my code is.
When I run the js file, I am getting following error:
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
Code is as follows:
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var currentTimeStamp = new Date();
var file = require (__dirname + '/functions');
mongodb.MongoClient.connect('mongodb://phx8b03c-fb1d-6.stratus.phx.ebay.com,phx8b03c-316d-6.stratus.phx.ebay.com,phx8b03c-9564-6.stratus.phx.ebay.com',
function (err, client) {
if (err) throw err;
client.createCollection('lbTopology' , function (err, collection) {
if (err) throw err;
collection.insert(file, 'lbTopology' , function (err) {
if (err) throw err;
client.close(function (err) {
if (err) throw err;
console.log('done');
});
});
});
});
Can someone please let me know what the issue is? Thanks a lot in advance
It looks like you don't have mongodb installed. Did you npm install mongodb in the same directory with your code or do you have a node_modules folder with mongodb in it?