Reputation: 2259
Is there way to append to the beginning of a file in node (maybe to insert). I was looking around her http://nodejs.org/api/fs.html but didn't see anything, I know that append brings adds to the end of the file, but is there a way to add to the beginning of a file?
Thanks
Upvotes: 1
Views: 2229
Reputation: 2754
You can do it the "unix" way with shelljs
and pipes.
const shell = require('shelljs')
shell
.echo('This line will be on top\n')
.cat('originalFile.txt')
.to('originalFile.txt')
Upvotes: 1
Reputation: 276286
No, there is no built in way to insert at the beginning of the file in nodejs.
This is nothing specific to nodejs, it's the same way in C C# Java Python and pretty much any other languages I know and is just how the file system works. ( here is why not by the way)
You can however, read the file with fs.readFile
and then write it back with the stuff you need added.
Upvotes: 2