Not works fs.readFile in node js

I have:

 fs.readFile('../services/Prescipcion.xml', "utf8", function (err, data) {
    console.log("err->", err);
    console.log("data", data);

And it logs:

  • createReadStream in Node.JS
  • How to access name of file within fs callback methods?
  • NodeJS File Statistics
  • How to reset nodejs stream?
  • error handling on readFileSync in node.js
  • Write JSON with query pseudo
  • err-> { 
      [Error: ENOENT: no such file or directory, open '../services/Prescipcion.xml']
      errno: -2,
      code: 'ENOENT',
      syscall: 'open',
      path: '../services/Prescipcion.xml' 

    I don’t understand why this happens.

  • nodejs write raw image data to jpeg file?
  • Node fs copy a folder
  • NodeJS File Statistics
  • create on fly zip file for download through node.js
  • how to create a readstream with a buffer using nodejs
  • JS build object recursively
  • 2 Solutions collect form web for “Not works fs.readFile in node js”

    The error message says no such file or directory, so at first sight this most likely means the path to the file is incorrect.

    Either the filename is incorrect (typo?) or the directory is incorrectly resolved. Take note that a relative path will be resolved against process.cwd():

    process.cwd(): Returns the current working directory of the process.

    You can try using console.log(process.cwd()) to help you debug the issue.

    If the file Prescipcion.xml should be retrieved locally from where the script is run, you can also use the following construct:

    fs.readFileSync(path.join(__dirname, '../services') + '/Prescipcion.xml', 'utf8');

    __dirname: The name of the directory that the currently executing
    script resides in.

    A full example that worked for me, based on other answer

    var fs = require('fs');
    var path = require('path');
    var readStream = fs.createReadStream(path.join(__dirname, '../rooms') + '/rooms.txt', 'utf8');
    let data = ''
    readStream.on('data', function(chunk) {
        data += chunk;
    }).on('end', function() {
    Node.js is the Best Javascript runtime in the world.