Electron “Uncaught ReferenceError: require is not defined”

夙愿已清 提交于 2021-01-28 19:51:25

问题


I'm trying to execute a file with this script:

 <script>
      function verify() {
    var child = require('child_process').execFile;
  var executablePath = "C:\\file";
  
  child(executablePath, function(err, data) {
  if(err){
     console.error(err);
     return;
  }
  
  console.log(data.toString());
    });
  }
    </script>

But when I run this script I get error "Uncaught ReferenceError: require is not defined". I've tried to fix this for 3 days with no sucess. I've enabled node intergration, installed browserify and read 10 diferrent explainations on how to do it with no success. Do anybody know a fix for this or a alternative on executing a file?


回答1:


See the Electron documentation.

You can only use require (and use the child_process module) from the main process, but you are trying to use it from the renderer process.

Move it to the main process.

If you need to trigger the function from the renderer process (e.g. when the user clicks on a button) then use the ipcRenderer module to send a message to the main process (and have a listener there which will call the verify function in response to that message).



来源:https://stackoverflow.com/questions/62681810/electron-uncaught-referenceerror-require-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!