Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  54] [ 1]  / answers: 1 / hits: 106171  / 12 Years ago, tue, march 27, 2012, 12:00:00

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:servertestserver.js. The test.txt is located in the same directory, but I get this error: Error: ENOENT, no such file or directory 'C:UsersUsertest.txt'


More From » node.js

 Answers
97

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

[#86583] Sunday, March 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;