nklayman / vue-cli-plugin-electron-builder

Easily Build Your Vue.js App For Desktop With Electron
https://nklayman.github.io/vue-cli-plugin-electron-builder/
MIT License
4.12k stars 278 forks source link

fs.existsSync is not a function #484

Closed ChunAllen closed 5 years ago

ChunAllen commented 5 years ago

Describe the bug I'm getting fs.existsSync is not a function when I'm importing electron in my component. This is similar to #335 but the solution is not working for me.

To Reproduce

import { remote } from 'electron';

const { app } = remote

export default {
  name: 'Home',
  methods: {
     restartApp() {
       app.relaunch();
       app.exit()l
     }  
  }
}

Expected behavior I should be able to relaunch the app by accessing the methods in my component.

Screenshots Screenshot 2019-09-23 18 24 45

Environment (please complete the following information):

nklayman commented 5 years ago

It works fine for me with the same code. Can you please create a sample repository that reproduces the issue and share it with me?

ChunAllen commented 5 years ago

It's working already I tried adding this in background.js

 win = new BrowserWindow({ width: 1040, 
                            height: 1900, 
                            fullscreen: true,
                            webPreferences: { preload: __static + '/preload.js' } 
                          })

then the preload.js

const remote = require("electron").remote;

window.restartApp = function() {
    console.log('Restarting');
    remote.app.relaunch()
    remote.app.exit(0);
};

I can now call the restartApp() inside the method in Vue component. But I think this is not an optimal solution because I'm adding the logic inside the public/

Any ideas how to make it more secured? or Why import { remote } from 'electron'; not being accessible in the first place.

MaDetho commented 5 years ago

got the same issue, any progress on this?

nklayman commented 5 years ago

Importing electron in a vue component works fine for me. Please create an example repo with the bug.

bruceauyeung commented 4 years ago

possibly related to this https://github.com/nklayman/vue-cli-plugin-electron-builder/blob/v2/docs/guide/configuration.md#node-integration i encountered same issue, and fixed it through that guide.

march1993 commented 4 years ago

possibly related to this https://github.com/nklayman/vue-cli-plugin-electron-builder/blob/v2/docs/guide/configuration.md#node-integration i encountered same issue, and fixed it through that guide.

Thanks a lot! I've been struggling for days and days.

march1993 commented 4 years ago

@nklayman May I ask why setting nodeIntegration in background.ts alone is not taking effect?

 win = new BrowserWindow({
   webPreferences: {
      nodeIntegration: true
    }
  });
nklayman commented 4 years ago

@march1993 It also has to adjust the webpack config to allow for native modules.

decpk commented 4 years ago

I’m facing the same error i.e fs.existsSync is not a function. I’ve tried everything and doesn’t work for me. I’ve got this error while importing or requiring the electron in vue component. Please help. @nklayman

//vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      nodeIntegration: true,
    },
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/styles/_variables.scss"; ',
      },
    },
  },
};
// CreateNewNote.vue
<script>
const { ipcRenderer } = require('electron');
export default {
  methods: {
    createNote() {
       ipcRenderer.send('createNewNote', 'This text is from renderer to background');
};
</script>

Still I get the same error. What else I'll do to get rid of this error.

nklayman commented 4 years ago

@and-ocean2017 I don't think it will help, but try import { ipcRenderer } from 'electron instead of what you have currently. There is a chance it might fix it.

decpk commented 4 years ago

Thanks, @nklayman I was trying running the electron application in dev mode i.e in the browser using (npm run serve) and obviously browser doesn't find the electron API in dev server, that's why I was getting that error. Then I took a closer look and solved this to run my application as (npm run electron:serve) i.e Compiles and hot-reloads for development. IF SOMEONE COULDN'T FIND THE SOLUTION TILL NOW SO TRY TO RUN APPLICATION USING (npm run electron:serve) or build using (npm run electron:build) .

cookmscott commented 4 years ago

It wasn't until I added nodeIntegration: true that my npm run serve broke. Does anyone know how to setup the project so you can run/build conditionally with the Electron API dependencies?

mavitm commented 4 years ago

I had the same problem too, I made arrangements as below, it worked.

electron main.js

win = new BrowserWindow({
    width: 1360,
    height: 768,
    webPreferences: {
      nodeIntegration: true,
      nodeIntegrationInWorker: true
    }

vue.config.js

module.exports = {
    pluginOptions: {
        electronBuilder: {
            nodeIntegration: true
        }
    }
    //...
}
AndreiSoroka commented 4 years ago

for me helped:

  1. package.json (up version, read more https://www.npmjs.com/package/electron-store)

    "electron-store": "^6.0.1",
  2. vue.config.js

    module.exports = {
    pluginOptions: {
        electronBuilder: {
            nodeIntegration: true, // <--- added
        }
    }
    //...
    }
  3. electron main.js

    win = new BrowserWindow({
    width: 1360,
    height: 768,
    webPreferences: {
      enableRemoteModule: true, // <--- added
      nodeIntegration: true,
    }
alisonmoura commented 3 years ago

As @MaviTm and @AndreiSoroka pointed out, doing those configurations worked for me.

In my case, I did:

On vue.config.js:

module.exports = {
  pluginOptions: {
    electronBuilder: {
      nodeIntegration: true,
    },
  },
};

On background.js's createWindow function:

 const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
      nodeIntegrationInWorker: true,
    },
  });

With that done, I could use the ipcRenderer as a module on a Vuejs SFC Component, as @nklayman sugested:

<script>
import { ipcRenderer } from "electron";

export default {
  name: "Home",
  methods: {
    start() {
      ipcRenderer.send("open", { msg: "Go!" });
    },
  },
};
</script>

And at last, but not least, as @and-ocean2017 said, should run the Electron Serve script, with npm run electron:serve or yarn electron:serve.

Thanks for the help guys :smile: !