69

I have a relatively small file (some hundreds of kilobytes) that I want to be in memory for direct access for the entire execution of the code.

I don't know exactly the internals of Node.js, so I'm asking if a fs open is enough or I have to read all file and copy to a Buffer?

1
  • Look at the readFile(), this is what you need. Commented Oct 31, 2013 at 12:06

3 Answers 3

106

Basically, you need to use the readFile or readFileSync function from the fs module. They return the complete content of the given file, but differ in their behavior (asynchronous versus synchronous).

If blocking Node.js (e.g. on startup of your application) is not an issue, you can go with the synchronized version, which is as easy as:

var fs = require('fs');

var data = fs.readFileSync('/etc/passwd');

If you need to go asynchronous, the code is like that:

var fs = require('fs');

fs.readFile('/etc/passwd', function (err, data ) {
  // ...
});

Please note that in either case you can give an options object as the second parameter, e.g. to specify the encoding to use. If you omit the encoding, the raw buffer is returned:

var fs = require('fs');

fs.readFile('/etc/passwd', { encoding: 'utf8' }, function (err, data ) {
  // ...
});

Valid encodings are utf8, ascii, utf16le, ucs2, base64 and hex. There is also a binary encoding, but it is deprecated and should not be used any longer. You can find more details on how to deal with encodings and buffers in the appropriate documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

small typo, missing comma after encoding in readFile function (I cant correct because the edit is less than 6 chars)
28

As easy as

var buffer = fs.readFileSync(filename);

Comments

17

With Node 0.12, it's possible to do this synchronously now:

  var fs = require('fs');
  var path = require('path');

  // Buffer mydata
  var BUFFER = bufferFile('../public/mydata');

  function bufferFile(relPath) {
    return fs.readFileSync(path.join(__dirname, relPath)); // zzzz....
  }

fs is the file system. readFileSync() returns a Buffer, or string if you ask.

fs correctly assumes relative paths are a security issue. path is a work-around.

To load as a string, specify the encoding:

return readFileSync(path,{ encoding: 'utf8' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.