70

I want to load test.txt with nodejs.

var fs = require('fs');
fs.readFile('./test.txt', function (err, data) {
  if (err) {
    throw err; 
  }
  console.log(data);
});

The path of the server is C:\server\test\server.js. The test.txt is located in the same directory, but I get this error: Error: ENOENT, no such file or directory 'C:\Users\User\test.txt'

Danny Fox
  • 38,659
  • 28
  • 68
  • 94

6 Answers6

92

Paths in Node are resolved relatively to the current working directory. Prefix your path with __dirname to resolve the path to the location of your Node script.

var fs = require('fs');
fs.readFile( __dirname + '/test.txt', function (err, data) {
  if (err) {
    throw err; 
  }
  console.log(data.toString());
});
Danny Fox
  • 38,659
  • 28
  • 68
  • 94
Rob W
  • 341,306
  • 83
  • 791
  • 678
20

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('../test.txt');

  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 fs.readFileSync(path,{ encoding: 'utf8' });
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
6

You should use __dirname to get the directory name the file is located instead of the current working directory:

fs.readFile(__dirname + "/test.txt", ...);
pimvdb
  • 151,816
  • 78
  • 307
  • 352
3

Use path and fs:

const fs = require("fs");
const pth = require("path");

Sync:

let data = fs.readFileSync(pth.join(__dirname,"file.txt"));
console.log(data + "");

A-Sync:

fs.readFile(pth.join(__dirname,"file.txt"), (err, data) => {
    console.log(data + "");
});

And that; If you need to read the file continuously and send it to the client and the file size is not large, you may be able to keep a copy of it:

const exp = require("express");
const app = exp();
const fs = require("fs");
const pth = require("path");

let file = "";
app.get("/file", (q, r) => {
    if (file === "")
        file = fs.readFileSync(pth.join(__dirname,"file.txt")) + "";

    r.writeHead(200, { "Content-Type": "text/plain" });
    r.write(file);
    r.end();
});
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
1

so if it is in the same directory just do this

 fs.readFile(__dirname+'/foo.txt',function(e,d){console.log(d)})
samccone
  • 10,746
  • 7
  • 43
  • 50
  • Rob it totally does .. just did it in node terminal – samccone Mar 27 '12 at 13:36
  • 2
    Create a `x.js` at `/tmp/x/x.js`. Put the OPs code in it. Also create `/tmp/x/test.txt`. Now, set your working dir to `/tmp` (eg. `cd /tmp`) and use `node x/x.js` or `node /tmp/x/x.js` --> `Error: ENOENT, no such file or directory 'test.txt'` – Rob W Mar 27 '12 at 13:39
  • 4
    Your original answer was effectively the same code as the OP has (which doesn't work), why the argument lol – Esailija Mar 27 '12 at 13:45
0

If it's in same directory it should work. I have tested with the same code, with a file name.txt and it's working fine:

var fs = require('fs');
fs.readFile('./test.txt', function (err, data) {
  if (err) {
    throw err;
  }
  console.log(data.toString());
});
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
nirmal
  • 51
  • 5