0

I'm trying to check if a file exist and if not, starts to download in my eletron app.

The error i have is the next one :

error - ./node_modules/electron/index.js:1:0
Module not found:can't resolve 'fs'
null

Where i use the function


 mainWindow.webContents.session.on(
    'will-download',
.....

   ipcMain.on('folder', (event, arg) => {
          if (arg === 'check') {
            const folder =
              app.getPath('userData') +
              '\\Local Storage\\' +
              privateHash.split('.')[0];
            var file = searchFile(folder);
            if (file != null) {
              OpenApp(
                folder + '\\' + file,
                meeting,
                token,
                company,
                avatar,
                scene
              );
            } else {
               sending a message to the renderer to download the file
              }

The searchFile function is something like this :

const searchFile = (folder) => {
  var fs = require('fs');
  var files = fs.readdirSync(folder.split('.')[0]);
   ....

I have looked in stackOverflow to similar problems and try their solutions, but for me it doesn't work.If somebody can help it will be great. I put nodeIntegration, in package.json browser..fs:false,path:false,os:false..,i add to next.config.js webpack config , nothing is working for me

2
  • please highligh code.
    – Spring
    Mar 29, 2022 at 10:42
  • I'm not sure, if this is related to your issue. Make sure the code runs only server side!
    – Stefan
    Mar 29, 2022 at 12:41

1 Answer 1

0

you are probably trying to run server side code on the browser, that is very common, i would suggest that you follow the calls stack and figure out where that code is being run a browser environment and and conditionally skip, EX

if (typeof window === 'undefined') {
  // do server side stuff ex. you can run your function here.
} else {
 // if you are in a client environment do nothing and return
 return;
}

1
  • Exactly or importing a function from a file that imports server packages.
    – I'm Jozi
    Mar 29, 2022 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.