問題描述
我正在嘗試使用此腳本執行文件:
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>
但是當我運行這個腳本時,我得到了錯誤Uncaught ReferenceError: require is not defined".我試圖解決這個問題 3 天沒有成功.我已經啟用了節點集成,安裝了 browserify 并閱讀了 10 個不同的解釋,說明如何做到這一點,但沒有成功.有人知道解決此問題或執行文件的替代方法嗎?
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?
推薦答案
參見 Electron 文檔.
您只能在 ma??in 進程中使用 require
(并使用 child_process
模塊),但您正試圖從渲染器進程.
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.
將其移至主進程.
如果您需要從渲染器進程觸發該功能(例如,當用戶單擊按鈕時),請使用 ipcRenderer
模塊 向主進程發送消息(并在那里有一個監聽器,它將調用 verify
函數來響應該消息).
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).
這篇關于Electron“未捕獲的引用錯誤:未定義要求"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!