0
I’m trying to read all the contents of a file .txt
through Electron possessing the following files:
script / index.js
index.html
file.txt
package json.
index js.
const fs = require('fs');
function read_file(){
try {
var data = fs.readFileSync('../file.txt', 'utf8');
console.log(data.toString());
} catch(e) {
console.log('Error:', e.stack);
}
}
HTML
<!DOCTYPE html>
<html lang="pt" dir="ltr">
<head>
<meta charset="utf-8">
<title>Reading File</title>
<script src="script/index.js"></script>
</head>
<body>
<button type="button" name="button" onclick="read_file()">Click me</button>
</body>
</html>
When executing the HTML Button, this error appears:
Error: Error: ENOENT: no such file or directory, open '.. /file.txt'
What’s wrong with this code that you can’t read the file via console?
And if you take the
../
from the file path, what happens?– Woss
It worked here, but why did I need to take this one (../) ? Because what came to mind was for index.js to read the previous directory.. how strange
– sYsTeM