Mac下 nodejs 及 electron 安裝


nodejs文檔:  http://nodejs.cn/api/
安裝nodejs
下載安裝包 或者 mac 下   brew install nodejs
 
安裝electron
nmp install -g electron
如果報錯     
Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/electron/electron-tmp-download-1374-1511880539207' npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! electron@1.7.9 postinstall: `node install.js` npm ERR! Exit status 1 npm ERR!
這是 /usr/local/ 目錄下的權限問題, 可以嘗試 sudo chmod 777  /usr/local/, 如果還是不行  可以嘗試關閉 csrutil
解決方案:
對於Mac OS X 10.11 El Capitan用戶,由於系統啟用了SIP(System Integrity Protection), 導致root用戶也沒有權限修改/usr/bin目錄。按如下方式可恢復權限。
屏蔽方法:重啟Mac,按住command+R,進入recovery模式。選擇打開Utilities下的終端,輸入:csrutil disable並回車,然后正常重啟Mac即可。
具體可見: http://www.howtogeek.com/2304...
 
 在electron項目中
報錯:require is not defined

修改創建BrowserWindow部分的相關代碼,設置屬性webPreferences.nodeIntegration為 true

let win = new BrowserWindow({
    webPreferences: {
        nodeIntegration: true
    }
})

 參考: https://www.cnblogs.com/kuku19940613/p/10814905.html

 

嗯  主界面的這個錯誤解決了  然而  嵌套的iframe 里面又報這個錯誤。  這每個頁面都要來這么一下么?

這是找遍了各種  google 百度都沒能解決我的問題  最典型的是以下的解決方案 這里也列出來 說不定能解決你們的問題呢?

// 在主進程中 const { BrowserWindow } = require('electron') let win = new BrowserWindow({ webPreferences: { nodeIntegration: false } }) win.show()

假如你依然需要使用 Node.js 和 Electron 提供的 API,你需要在引入那些庫之前將這些變量重命名,比如:<head>

<script> window.nodeRequire = require; delete window.require; delete window.exports; delete window.module; </script> <script type="text/javascript" src="jquery.js"></script> </head>
以上方法都用了 然鵝依然沒有解決我的問題
最終在這里找到了我的解決方案

參考: https://stackoverflow.com/questions/45255773/electron-iframe-require-is-not-defined

iframe.onload = function () { const iframeWin = iframe.contentWindow iframeWin.require = window.require })
也就是在我的工程中
在主進程中
// 在主進程中
const { BrowserWindow } = require('electron')
let win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
})
win.show()

在iframe嵌套的地方

var node_frame = document.createElement("iframe");
node_frame.onload = function () {
    node_frame.contentWindow.require = window.require
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM