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...
屏蔽方法:重启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 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现