ERROR: No README.md file found!

npm install tie-file
2 downloads in the last week
5 downloads in the last month


A port(ish) of Tie::File, this creates an array-like object with the contents of a file on disk, where all modifications are written to the file automatically.


var tieFile = require('tie-file')

// File will be created on first modification if it doesn't exist
var file = tieFile(filename)

file.set(13, 'blah');      // line 13 of the file is now 'blah'
console.log(file.get(42)); // display line 42 of the file

nRecs = file.length;      // how many records are in the file?
file.splice(nRecs - 3, 2) // chop two records off the end

// Replace javascript with JavaScript everywhere in the file
file.forEach(function (line) {
  line.inPlace(/javascript/i, 'JavaScript')

// These are just like regular push, pop, unshift, shift, and splice
// Except that they modify the file in the way you would expect

var r1 = file.pop();
var r2 = file.shift();
var oldRecs = file.splice(3, 7, recs...);

// file is an EventEmitter
file.on('error', function (err) { ... })


Very incomplete, untested, something of a PoC at this point.

In-place modification of lines

Line objects act like strings but add a few extra methods that allow them to be modified in-place:


Replace the line with string.

inPlace(pattern, replacement)

Just like String.prototype.replace, but will swap the line with the return value.

{prepend, append}(string...)

Places one or more strings at the start/end of the line.


Array access doesn't work (yet?), for now you must use the get/set methods. I'm not sure how best to do this without resorting to ES6 proxies.



npm loves you