问题
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