D
D
Dubolom Unicellular2020-06-09 16:49:26
JavaScript
Dubolom Unicellular, 2020-06-09 16:49:26

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

I got an error:
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

By the way, I'm using webpack.
I rummaged through the Internet, I tried 7 options on how to fix this, but, all in vain. Here is the best option:
const fs = require('fs');
const path = require('path');

let fileData = fs.readFileSync(path.join(__dirname, "text.txt")).toString();
console.log(fileData);

After this most "successful option" another error occurred:
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

After that, I realized that it smells like kerosene.
Tried to just create a text2.txt file:
const fs = require("fs");

fs.writeFile("text2.txt", "Привет, soso!");


Got the error:
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


What should I do?
I already included nodeIntegration in main.js!
Here is my package.json:
{
  "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"
  }
}


But if you just run a file with text.txt read, then everything works (using the command on the command line: node app.js)

------------------------ ------------------------
Answer that worked for me:

EDIT (leaving a note for those who also had this problem):
I found the answer , it didn't work because of __dirname, because of webpack, it gave me just / :

console.log(__dirname); // выдавал мне /  (  / - это символ слеш если что)

It was necessary to write in webpack.conf.js:
node: {
  __dirname: true
}

In general, it's better (if you're using nodeJS) to set the global: true property (as well as __filename): - this will make all nodejs functions valid, or in other words, won't prevent nodejs from doing its job:
node: {
  global: true,
  __dirname: true,
  __filename: true
}

More about the node property in webpack.conf.js here: https://webpack.js.org/configuration/node/
After that, I tried to output __dirname:
console.log(__dirname); // выдает src/assets/js/app.js

Here is a working readFileSync:
let fileData = fs.readFileSync(__dirname + "/text.txt").toString();
console.log(fileData); // выдает: "Привет, soso!"


In general, the fs itself worked, in writeFile, I just did not specify the error handling callback in the 2nd parameter (or the 3rd if you pass an additional "utf-8" parameter) err => if(err) console.log(err );
Example:
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) });

More details (and also this is where I found info about webpack blocking __dirname):
https://github.com/webpack/webpack/issues/1599

So here... if I find anything else about it, I will add here.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question