71

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'

6 Answers 6

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()); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting ReferenceError: __dirname is not defined. Is it because I sould manually define var __dirname = '/the/path/' ?
@myfirsttime1 __dirname only works in Node scripts, not in the REPL.
22

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' }); 

Comments

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", ...); 

Comments

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(); }); 

Comments

1

so if it is in the same directory just do this

 fs.readFile(__dirname+'/foo.txt',function(e,d){console.log(d)}) 

3 Comments

Rob it totally does .. just did it in node terminal
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'
Your original answer was effectively the same code as the OP has (which doesn't work), why the argument lol
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()); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.