Answer the question
In order to leave comments, you need to log in
Completely cut off fs in Electron, what to do (Solved)?
(I found the answer to this question, the solution is detailed at the bottom of the question)
Good afternoon!
Fs doesn't work for me in electron. I noticed this when I just wrote in app.js:
const fs = require('fs');
let fileData = fs.readFileSync("text.txt").toString();
console.log(fileData);
internal/fs/utils.js:219 Uncaught Error: ENOENT: no such file or directory, open 'text.txt'
at Object.openSync (fs.js:440)
at Object.func [as openSync] (electron/js2c/asar.js:140)
at Object.readFileSync (fs.js:342)
at Object.fs.readFileSync (electron/js2c/asar.js:542)
at Module../src/assets/js/app.js (app.js:12)
at __webpack_require__ (bootstrap:19)
at Object.0 (app.scss?3676:19)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
const fs = require('fs');
const path = require('path');
let fileData = fs.readFileSync(path.join(__dirname, "text.txt")).toString();
console.log(fileData);
Uncaught Error: ENOENT: no such file or directory, open '\text.txt'
at Object.openSync (fs.js:440)
at Object.func [as openSync] (electron/js2c/asar.js:140)
at Object.readFileSync (fs.js:342)
at Object.fs.readFileSync (electron/js2c/asar.js:542)
at Module.<anonymous> (app.js:13)
at Module../src/assets/js/app.js (app.js:17)
at __webpack_require__ (bootstrap:19)
at Object.0 (app.scss?3676:19)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
const fs = require("fs");
fs.writeFile("text2.txt", "Привет, soso!");
Uncaught TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
at maybeCallback (fs.js:135)
at Object.writeFile (fs.js:1234)
at Module../src/assets/js/app.js (app.js:13)
at __webpack_require__ (bootstrap:19)
at Object.0 (app.scss?3676:19)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
{
"name": "electron",
"productName": "electron",
"version": "1.0.0",
"description": "My Electron application description",
"main": "electron/main.js",
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "echo \"No linting configured\"",
"dev": "webpack-dev-server --open --config ./webpack/webpack.dev.conf.js",
"build": "webpack --config ./webpack/webpack.build.conf.js"
},
"keywords": [],
"author": "",
"license": "MIT",
"config": {
"forge": {
"packagerConfig": {},
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "electron"
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
]
},
{
"name": "@electron-forge/maker-deb",
"config": {}
},
{
"name": "@electron-forge/maker-rpm",
"config": {}
}
]
}
},
"dependencies": {
"electron-squirrel-startup": "^1.0.0"
},
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.51",
"@electron-forge/maker-deb": "^6.0.0-beta.51",
"@electron-forge/maker-rpm": "^6.0.0-beta.51",
"@electron-forge/maker-squirrel": "^6.0.0-beta.51",
"@electron-forge/maker-zip": "^6.0.0-beta.51",
"css-loader": "^3.5.3",
"electron": "9.0.2",
"file-loader": "^6.0.0",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.14.1",
"sass-loader": "^8.0.2",
"style-loader": "^1.2.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0",
"webpack-node-externals": "^1.7.2"
}
}
console.log(__dirname); // выдавал мне / ( / - это символ слеш если что)
node: {
__dirname: true
}
node: {
global: true,
__dirname: true,
__filename: true
}
console.log(__dirname); // выдает src/assets/js/app.js
let fileData = fs.readFileSync(__dirname + "/text.txt").toString();
console.log(fileData); // выдает: "Привет, soso!"
const fs = require("fs");
fs.writeFile("/text2.txt", "Привет", err => { if(err) console.log(err) });
// или
const fs = require("fs");
fs.writeFile("/text2.txt", "Привет", "utf-8", err => { if(err) console.log(err) });
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question