TypeError: fs.existsSync is not a function | import { ipcRenderer } from 'electron'
在electron的渲染进程中导包会发生TypeError: fs.existsSync is not a function node_modules/electron/index.js:6
var pathFile = path.join(__dirname, 'path.txt')
if (fs.existsSync(pathFile)) {
module.exports = path.join(__dirname, fs.readFileSync(pathFile, 'utf-8'))
} else {
throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
}
产生问题的原因:
1、首先在渲染进程属于浏览器端,没有集成Node的环境,所以类似 fs 这样的Node的基础包是不可以使用。
2、因为没有Node环境,所以require关键词是不可以使用的。
弄清楚这个就一起解决问题吧:
方案一:
渲染进程
const { ipcRenderer } = window.require('electron');
主进程
const win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})
使用这种方式能够是electron为前端工程提供Node的环境,让程序能够正常运行。
但是,单独启动前端工程会出现 window.require is not a function .
方案二:
来源于StackOverflow
1、创建 preload.js 文件:
window.ipcRenderer = require('electron').ipcRenderer;
2、在main.js文件中的 webPreferen中设置预加载preload:
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
preload: __dirname + '/preload.js'
}
});
3、渲染进程
componentDidMount() {
if (isElectron()) {
console.log(window.ipcRenderer);
window.ipcRenderer.on('pong', (event, arg) => {
this.setState({ipc: true})
})
window.ipcRenderer.send('ping')
}
}
is-electron for the isElectron() function
方案三:
来源以 github
如果你使用TypeScript可以这样做:
import {IpcRenderer} from 'electron';
declare global {
interface Window {
require: (module: 'electron') => {
ipcRenderer: IpcRenderer
};
}
}
const { ipcRenderer } = window.require('electron');
方案四:
读了N个electron项目后,写成的一种解决方案:
直接上代码
externals(context, request, callback) {
const isDev = process.env.NODE_ENV === 'development';
let isExternal = false;
const load = [
'electron',
'fs',
'path',
'os',
'url',
'child_process'
];
if (load.includes(request)) {
isExternal = `require("${request}")`;
}
const appDeps = Object.keys(require('./app/package').dependencies);
if (appDeps.includes(request)) {
const orininalPath = slash(join(__dirname, './app/node_modules', request));
const requireAbsolute = `require('${orininalPath}')`;
isExternal = isDev ? requireAbsolute : `require('${request}')`;
}
callback(null, isExternal);
},
发现了吗,在前端工程配置的时候,默认设置externals参数,在使用require的时候会查看默认加载的模块中有没有,按需加载模块,如果初始加载的模块中没有该模块,会向上级目录./app/package查找模块。
TypeError: fs.existsSync is not a function | import { ipcRenderer } from 'electron'的更多相关文章
- TypeError: window.open is not a function
想必大家现在都已经到家了,而苦逼的我还要坐在办公室混拿微薄的工资,技不如人,平常不努力给自己充电,年终一毛钱都没多给.不说这扫兴的话题了,在这给同样在苦逼坚守岗位的同志们节日的问候,新的一年,好运连连 ...
- Meteor错误:TypeError: Meteor.userId is not a function
问题描述: 浏览器console提示错误TypeError: Meteor.userId is not a function. 原因分析: 通过查看Meteor API文档,可知该函数由包accoun ...
- extjs之TypeError: d.read is not a function解决方案
在创建如下代码时报出此错:TypeError: d.read is not a function Ext.define('shebyxgl_sheb_model', { extend: 'Ext.da ...
- TypeError: value.getTime is not a function (elementUI报错转载 )
"TypeError: value.getTime is not a function" 2018年07月02日 16:41:24 leeleejoker 阅读数:2091 标签: ...
- Entity Framework 5.0.0 Function Import 以及 ODP. NET Implicit REF CURSOR Binding使用简介
源代码 概要: 1,说明如何使用Entity Framework中的function import功能. 2,说明如何使用ODP.NET的隐式REF CURSOR绑定(implicit REF CUR ...
- TypeError: Buffer.alloc is not a function
错误信息:TypeError: Buffer.alloc is not a function 截图如下: 解决办法(依次从上往下执行): sudo npm cache clean -f sudo np ...
- jquery.js 3.0报错, Uncaught TypeError: url.indexOf is not a function
转载自:http://majing.io/questions/432 问题描述 jQuery升级到3.0.0后类型错误 jquery.js:9612 Uncaught TypeError: url ...
- Uncaught TypeError: (intermediate value)(...) is not a function 上一个方法结束没有加分号; 代码解析报错
Uncaught TypeError: (intermediate value)(...) is not a function 别忽略了, 第一个方法后面的结束 分号; 不起眼的,引来麻烦, 哎,规 ...
- jquery3.1.1报错Uncaught TypeError: a.indexOf is not a function
jquery3.1.1报错Uncaught TypeError: a.indexOf is not a function 使用1.9就没有问题,解决办法: 就是把写的代码中: $(window).lo ...
- JQuery中button提交表单报TypeError: elem[type] is not a function jquery
错误: TypeError: elem[type] is not a function jquery 解决: 出现这种现象的原因是,提交的表单中,有标签的name,有以submit命名的 name中不 ...
随机推荐
- 代码随想录Day17
654.最大二叉树 给定一个不重复的整数数组 nums . 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点,其值为 nums 中的最大值. 递归地在最大值 左边 的 子数组前缀 ...
- 使用 Microsoft Remote Desktop 远程连接 Windows
Windows 使用 Microsoft 帐户登录 远程连接时使用的用户名和密码是你的 Microsoft 帐户的用户名和密码 Windows 使用本地帐户登录 远程连接时使用的用户名和密码是你本地登 ...
- golang协程的最佳实践与context包的基本使用
1.协程最佳实践,多个协程并发求素数 点击查看代码 var wg sync.WaitGroup func Prime(num int) bool { if num == 1 { return fals ...
- Docker高级:Redis集群实战!4主4从缩容到3主3从,怎么处理?
在上一篇,我们学会了redis集群的扩容.从3主3从扩容到4主4从. 那么,接着,活动过去了.流量没有那么大了.需要缩容了.从4主4从缩容到3主3从了.那么这个时候又该怎么处理呢? PS本系列:< ...
- 计算QPS-Sentinel限流算法
sentinel 前方参考 计算QPS-Sentinel限流算法 https://www.cnblogs.com/yizhiamumu/p/16819497.html Sentinel 介绍与下载使用 ...
- 04. 寻找两个正序数组的中位数 Golang实现
题目: 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 算法的时间复杂度应该为 O(log (m+n)) . 示例 1: ...
- CSS – z-index
介绍 z-index 是用来设置 element 层次高低的 (当 element 重叠的时候) 参考: 4 reasons your z-index isn't working (and how t ...
- Figma 学习笔记 – 黑科技
Figma 其实有蛮多黑科技的, 因为需求真的太多了, 在还没有实现的期间就诞生了很多 hacking 的 way. Feature Issue 实现 min-height 通过 0px 黑科技 其实 ...
- 【原创】解决NasCab掉进程,NasCab进程维护
最近对象吐槽家里服务器又连不上,看不了考研视频了. 我掏出手机一试,确实连不上.家里的服务器是Win11平台,用NasCab管理的视频文件,然后通过frpc做的内网穿透. 我们在外面的图书馆,连不上无 ...
- 服务器文件打压缩包下载(java)
public void download(HttpServletRequest request, HttpServletResponse response){ try { String downloa ...