07_05_GuyT
07_05_GuyT

Reputation: 2887

Error to read file from C drive with fs

I use the following code to read txt file from my C drive and I got error

fs = require('fs')


    var path = require('path');

    var filePath = path.join(__dirname, '‪C://nodeTest//test.txt');
    fs.readFile(filePath, 'utf8', function (err,data) {
        if (err) {
            return console.log(err);
        }

    });

in addition I try like following with the same error

fs.readFile('‪C://nodeTest//test.txt', 'utf8', function (err,data) {
        if (err) {
            return console.log(err);
        }

    });

error is

Error: ENOENT, open 'C:\nodeTest\test']
  errno: -4058,
  code: 'ENOENT',

My project is under the following path

C:\Users\C015869\WebstormProjects\myApp\server.js

Upvotes: 0

Views: 3635

Answers (2)

Yoseph
Yoseph

Reputation: 2416

  • Make sure the file exist with fs.existsSync('‪C://nodeTest//test.txt')
  • Make sure you have admin privilege. If you are on windows run Command Line as Administrator
  • Use single slash: var filePath = 'C:/nodeTest/test.txt'; or var filePath = path.join(__dirname, '‪test.txt')

Upvotes: 2

JBux
JBux

Reputation: 1404

You can only read from the webserver. If the file isn't somewhere in the webserver directory or subdirectories you can't read it. If the webserver directory is nodeTest then just use:

fs = require('fs')

var path = require('path');

var filePath = path.join(__dirname, '‪test.txt');
fs.readFile(filePath, 'utf8', function (err,data) {
    if (err) {
        return console.log(err);
    }

});

Upvotes: -1

Related Questions