node.js - 如何在Electron App中查看文件?

标签 node.js electron

我想使用 Chokidar 来观看我的应用中的文件。问题是,每当您尝试实例化 chokidar.watch 时,Electron 就会抛出错误。我想知道这是否是两者之间已知的兼容性问题,以及是否有内置解决方案可以解决此问题。到目前为止,谷歌还没有透露任何有关在 Electron 应用程序中观看文件的信息。该错误仅在使用 chokidar 对象时发生。要求它不会引起问题。

我在渲染器进程中的代码如下。注释掉 var watcher 代码将完全消除控制台错误。该代码直接来自他们的文档。

var chokidar = remote.require('chokidar');
chokidar.watch('.', {ignored: /[\/\\]\./}).on('all', function(event, path) {
  console.log(event, path);
});

控制台错误:

Uncaught Error: criterion.test is not a function
TypeError: criterion.test is not a function
at testCriteria (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/node_modules/anymatch/index.js:29:26)
at Array.some (native)
at anymatch (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/node_modules/anymatch/index.js:59:48)
at EventEmitter.FSWatcher._isIgnored (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/index.js:221:15)
at EventEmitter.NodeFsHandler._addToNodeFs (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/lib/nodefs-handler.js:429:12)
at EventEmitter.<anonymous> (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/index.js:410:12)
at /Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/node_modules/async-each/index.js:16:7
at Array.forEach (native)
at each (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/node_modules/async-each/index.js:15:11)
at EventEmitter.FSWatcher.add (/Users/guest/Desktop/prototype/yeti-launch/node_modules/chokidar/index.js:409:5)

非常感谢有关如何将观看功能添加到我的项目中的任何建议。 Atom 编辑器使用文件资源管理器检测何时添加文件和文件夹,并在侧边栏中适本地显示最新的文件夹信息。如果 Atom 可以做到,我的应用程序也可以!

最佳答案

这可能违反了 Electron 远程对象的所有规则 - 但您可以尝试访问 chokidar 的远程版本:

var chokidar = global\['require'\]('remote').require('chokidar');

关于node.js - 如何在Electron App中查看文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30787590/

相关文章:

javascript - 如何自动化使用Electron框架开发的桌面应用程序?

javascript - 使用 Electron Build 捆绑 MSI 安装程序

node.js - 将 OpenCV Mat 渲染到 Html5 Canvas 的最佳方法?

node.js - Node js - 快速发送 pdf 作为响应下载

javascript - 为什么我得到 "UnhandledPromiseRejectionWarning"?

javascript - Node.js错误: Can't set headers after they are sent.

javascript - Node 的模块 async 没有按预期工作

node.js - 无法使用 Electron 构建器构建应用程序。错误: gyp failed with exit code: 1

javascript - 我能够安全地访问特定的 Electron API 吗?

electron - Electron 应用程序在Ubuntu开发过程中是否需要 Chrome