I have below code which have to create html file with run time values.
var fs = require('fs');
const html = generateHtml(content); //Its successfully generated html content
fs.writeFileSysnc(buildPathHtml, html,(err) => {
if(err)
{
throw err;
console.log(err)
}
});
log.info('Succesfully created an HTML Report at ' + buildPathHtml);
I set the debug mode and I can see the entire .html structure that stored in html const in the above code and buildPathHtml is the location of the file.
But when I run the code and the below error is observed in output console. And the content is failed to write.
Output Console:
Error: ENOENT: no such file or directory, open 'c:\Users\arrchanaMohan\testCode\IncentRacktor\Test\shark\Reports\SANITY_2020-06-26:08:32:56.html'
Build Path:
c:\Users\arrchanaMohan\testCode\IncentRacktor\Test\shark\Reports\SANITY_2020-06-26:08:32:56.html
Appending the timestamp after 'SANITY_'
Kudos to #Teemu
I just removed colon (:) and used _ in the build path. And It worked.
Related
My code is as below. The contents of the file is a simple "hello world" I have the hello.docx file in the same folder I am calling this mammoth function.
Error result: fatal Error: ENOENT: no such file or directory, open './hello.docx'
Any idea what I am doing wrong? I am using this in my express node route
mammoth.extractRawText({path: "./hello.docx"})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
// var messages = result.messages;
})
.done();
It seems clear by the error displayed that the directory or file cannot be read. How are you uploading your file? If you are uploading your docx file using multer, you need to provide a reference to the file uploaded inside the path or buffer option as such:
mammoth.extractRawText({buffer: variable_that_holds_the_file.buffer})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
})
.done();
Else, you need to revise the path since it may not be correct.
To test this, use __dirname and or __filename in your console.log inside multer to see where your path is.
use below code it might help you. it worked for me.
const mammoth = require("mammoth");
const path = require("path");
let filePath = path.join(__dirname,'./sampledocxfile.docx');
mammoth.extractRawText({path: filePath})
.then(function(result){
var html = result.value;
var messages = result.messages;
console.log(html);
})
.done();
I'm building a script that reads log files, handles what needs to be handled then writes them to a database
Some caveats :
Some log files have a lot of input, multiple times a second
Some log files have few to no input at all
What I try in simple words:
Reading the first line of a file, then deleting this line to go to the next one, while I handle the first line, other lines could be added..
Issues I'm facing
When I try reading a file then processing it, then deleting the
files, some lines have been added
When the app crashes while
handling multiple lines at once for any reason, I can't know what
lines have been processed.
Tried so far
fs.readdir('logs/', (err, filenames) => {
filenames.forEach((filename) => {
fs.readFile('logs/'+filename, 'utf-8', (err, content) => {
//processing all new lines (can take multiple ms)
//deleting file
fs.unlink('logs/'+filename)
});
});
});
Is there not a (native or not) method to 'take' first line(s), or take all lines, from a file at once?
Something similar to what the Array.shift() method does to arrays..
Why you are reading the file at once. Instead you can use the node.js streams.
https://nodejs.org/api/fs.html#fs_class_fs_readstream
This will read the files and output to console
var fs = require('fs');
var readStream = fs.createReadStream('myfile.txt');
readStream.pipe(process.stdout);
You can also go for the npm package node-tail to read the content of a files while new content written to it.
https://github.com/lucagrulla/node-tail
If your log files has been writen as rotate logs. Example: Each hours has each log file, 9AM.log, 10AM.log....When you process the log files, you can skip current file and process another files. ex: now is 10:30 AM o’clock, skip file 10AM.log, solve another files.
I am copying a .jpg file to other directory(C:\myFaceApp\dropbox\exprtedFaces)
My source File : C:/myFaceApp/dropbox/faces/Monika1/1404039d-2be3-43bc-b20b-35c0f4a5954b/1404039d-2be3-43bc-b20b-35c0f4a5954b_00-00-04_crop.jpg
I am using following block of code to copy
targetPath=opts.exportDir; //C:\myFaceApp\dropbox\exprtedFaces
fs.createReadStream(req.query.facePath).pipe(fs.createWriteStream(targetPath));
res.write(JSON.stringify({ OK: 1 }));
res.end();
I am getting an error like this:
Error: EISDIR: illegal operation on a directory, open 'C:\myFaceApp\dropbox\exprtedFaces'
Your problem is that you are attempting to write to a directory not a file. createWriteStream takes a filename as it's argument. Try this instead:
fs.createReadStream(req.query.facePath).pipe(fs.createWriteStream(path.join(targetPath ,"file.jpg")));
You should ofc give it a non hard coded name, this is just an example. Have a look at the path module for that.
I am using node js express. I am trying to access a text file located in the same directory of that js file. So the file structure goes like this
- ProjectFolder
|
- many modules and folders
- routes
|
- Index.js
- input.txt
The Simple code that i have tried is ,
var data = fs.readFile('~/IdeaProjects/Title/routes/input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
I did try different paths but nothing works. for your information, I am using fedora as os.
The error i got was,
Error: ENOENT: no such file or directory, open '~/IdeaProjects/Title/routes/input.txt'
at Error (native)
Any suggestion about how to access that file so that i can both read and write the contents of the file , will be welcomed. Looking for detailed answer.
Node doesn't interpret some characters that have special meaning like ~ or shell variables like $HOME, so you will need to use something like path.resolve() to get an absolute path or use a relative path (e.g. IdeaProjects/Title/routes/input.txt).
Also, as #Gothdo pointed out, there is a discrepency in the filename which will cause issues on case-sensitive file systems.
You will also need to either change fs.readFile() to fs.readFileSync() or add a callback to fs.readFile() like so:
fs.readFile('~/IdeaProjects/Title/routes/input.txt', function(err, data) {
if (err) throw err;
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
});
I have a Node.js script that reads the contents of a file, does some transformations on its contents, and logs the output:
var transformer = require('./transformer'),
fs = require('fs'),
file = process.argv[2];
if (!file) {
throw 'no file specified\n';
}
fs.readFile(file, 'utf-8', function (err, data) {
if (err) {
throw err;
}
transformer.transform(data, function (text) {
console.log(text);
});
});
This works fine:
$ node transform.js myfile.txt
And this works:
$ node transform.js myfile.txt > anotherfile.txt
But, when I try to redirect the output to the same file I'm reading from, the file becomes blank:
$ node transform.js myfile.txt > myfile.txt
Same thing using tee:
$ node transform.js myfile.txt | tee myfile.txt
Curiously, this works:
$ node transform.js myfile.txt >> myfile.txt
But I don't want to append to the file - I want to overwrite its contents.
I think the problem is, since fs.readFile is asynchronous, console.log is called asynchronously as well - i.e., it gets chunks of data as opposed to all the data at once. I think I can use fs.readFileSync instead, but what's the right way to handle this?
The issue is not actually within Node but in the shell. When you redirect with >, the first thing the shell does is open the file for writing, emptying the file. Your program goes to read from that empty file and, in your case, empty input means empty output.
This too will result in an empty file regardless of the initial contents of myfile.txt:
$ cat myfile.txt > myfile.txt
One solution would be to write the file inside the Node script rather than using redirection. You're already specifying and reading the file there, so why not specify an output file in argv as well and write to it rather than using shell redirection? Just take care to structure your code so that reading and writing to the same file works.
As #slebetman notes in a comment, another solution is cat myfile.txt > tmp; mv tmp myfile.txt (or my preferred: cat myfile.txt > tmp && mv tmp myfile.txt).
The problem is
you're opening the file for read,
then opening the file for write (emptying it),
then reading from an empty file.
transform nothing
write nothing
What I think you want instead is to:
open for read
read and buffer
transform
open for write
write
There's a couple ways to do this:
1) Read the file synchronously. Node.js 0.12 supports this.
var transformer = require('./transformer'),
fs = require('fs'),
file = process.argv[2];
if (!file) {
throw 'no file specified\n';
}
fs.readFileSync(file, 'utf-8', function (err, data) {
if (err) {
throw err;
}
transformer.transform(data, function (text) {
console.log(text);
});
});
2) Use "streams"
This is really the best way. Especially if you're wanting to learn Node.js
The best way I know to learn about streams is from NodeSchool: http://nodeschool.io/#workshoppers Try the stream-adventure.
By the end, you'll own these kinds of problems.
Good luck!