Reputation: 263
I am trying to read a file from local system using node js 'fs' module. But for some reason, the 'fs' module is not working when I pass absolute path.
Code:
let filePath = "/home/mysystem/dev/myproject/sayHello.txt";
let newFile=fs.readFileSync('file://'+filePath);
The code is throwing an error as:
Uncaught Error: ENOENT: no such file or directory, open 'file:///home/mysystem/dev/myproject/sayHello.txt'
But I can open the file from browser window using the same path. fs module is working if I pass relative path. I am using this inside an app built using electron framework.
Upvotes: 6
Views: 21906
Reputation: 1179
I got stumped with this for a bit. If using Angular 2 and Typescript on Windows, your absolute path will look like this:
import { readdirSync } from "fs";
. . .
let x = readdirSync("C:/SAFE/Dir1/Blah");
console.log("files retrieved="+ x.length);
Upvotes: 2
Reputation: 383
In NodeJS you don't have to use the file
protocol for reading files.
You can get rid of the "file://"
part and try read the filePath
directly
let filePath = "/home/mysystem/dev/myproject/sayHello.txt";
let newFile = fs.readFileSync(filePath);
Upvotes: 8