0

I am trying to use the fs module in my Electron app to copy and edit files, however it does not work in any way.

I have a schedule.js file where I want to have methods to copy files.

I tried adding const fs = require('fs'); and using fs.copyFile() in my code, ended up telling me Uncaught ReferenceError: require is not defined at schedule.js:2:12.

I found the solution of doing import { copyFile } from "fs"; (or "original-fs"), it told me Uncaught TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../"..

I tried importing it to Electron's index.js (where imports seem to go fine) and then exporting it to schedule.js, it did not work. I tried changing package.json to "type" : "module", it did not work. I tried changing package.json to "type" : "commonjs", it did not work.

schedule.js

// const fs = require("fs");
import { copyFile } from "fs";

function fsCopyFile(source, destination) {
    copyFile(source, destination, (err) => {
      if (err) throw err;
      console.log(source+' was copied to '+destination);
  });
  }

index.js

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      // devTools: false
      nodeIntegration: true
    },
    frame: false,
    titleBarStyle: 'hiddenInset'
  });
  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));
};

Thanks in advance for those of you who can help me, I have been working on it for 2 days.

Diocrasis
  • 1
  • 1
  • @AlexanderLeithner adding `contextIsolation: false` worked, thank you very much, you have no idea how much of a burden it was – Diocrasis Dec 14 '22 at 11:45

0 Answers0