electron,browserify之后,fs.existsSync不是函数

electron, after browserify, fs.existsSync is not a function(electron,browserify之后,fs.existsSync不是函数)

本文介绍了electron,browserify之后,fs.existsSync不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于 browserify 和 electron 以及 gui 浏览器问题的信息但是browserify还是有问题,说fs.existsSync不是函数",required没有定义"

* 全文*我用电子创建了简单的 gui,有 package.json 文件、main.js 和 index.html 文件 + 3,4 个 html 文件,我想在其中创建与 require 一起使用的简单加载显示保存窗口"

该功能在 index.html 文件中有效,但在 load.html 文件中无法正常工作,因此我使用命令浏览 main.js

var fs = require('electron')//console.log(require('fs').existsSync);var remote = require('electron').remote;//var remote = require('remote');var dialog = require('electron').remote 

进入 main.bundle3.js,使用,(在 cmd 中)

浏览 main.js >main.bundle3.js

然后 load.html 文件大喊 require 没有定义和

<代码>>main.bundle3.js:6945 Uncaught TypeError: fs.existsSync 不是函数在对象.<匿名>(main.bundle3.js:6945)在 Object.require.36.fs (main.bundle3.js:6951)在 s (main.bundle3.js:1)在 main.bundle3.js:1在对象.<匿名>(main.bundle3.js:6794)在 Object.require.35._process (main.bundle3.js:6937)在 s (main.bundle3.js:1)在 e (main.bundle3.js:1)在 main.bundle3.js:1(匿名)@ main.bundle3.js:6945要求.36.fs @ main.bundle3.js:6951s@main.bundle3.js:1(匿名)@ main.bundle3.js:1(匿名)@ main.bundle3.js:6794要求.35._process @ main.bundle3.js:6937s@main.bundle3.js:1e@main.bundle3.js:1(匿名)@ main.bundle3.js:1

package.json

{名称":RDF",版本":0.1.0","main": "main.js",脚本":{测试":摩卡 -u 出口 -R 规格测试/索引"},开发依赖":{电子":^1.6.2",电子包装器":^8.6.0","html-browserify": "0.0.6",jquery":^3.2.1"}}

和 load.html 文件

<!DOCTYPE html><html><头><meta charset="UTF-8"><script src="https://cdn.plot.ly/plotly-latest.min.js"></script><!--<script src="main.js"></script>--><script src="main.bundle3.js"></script><!-- <script type="text/javascript" src="main.js"></script>--><script type="text/javascript" src="./lib/jquery-1.7.2.min.js"></script></头><身体><h3>加载</h3><p>加载数据</p><!--<input type="button" value="Details" onclick="javascript:$('#mainContainer').load('index.html');"/><br><div id="mainContainer"></div>--><div id="tab33">

<div style="text-align:center;"><input type="text" placeholder="只选择一个文件" id="actual-file" disabled="disabled"/><input type="button" value="选择一个文件" id="select-file"/></div><br><br><textarea id="content-editor" rows="5"></textarea><br><br><input type="button" id="save-changes" value="保存更改"/><input type="button" id="delete-file" value="删除文件"/></div><小时><div style="text-align:center;"><p>他的文件内容将与编辑器相同.</p><input type="button" value="选择一个文件" id="create-new-file"/></div><脚本>var fs = 要求('fs')var {remote} = require('electron').remote ;var {dialog} = require('electron').remotedocument.getElementById('select-file').addEventListener('click',function(){dialog.showOpenDialog(函数(文件名){如果(文件名 === 未定义){console.log("没有选择文件");}别的{document.getElementById('actual-file').value = fileNames[0];readFile(fileNames[0], fileReadComplete);}});},错误的);//document.getElementById('save-changes').addEventListener('click',function(){var actualFilePath = document.getElementById("actual-file").value;如果(实际文件路径){saveChanges(actualFilePath,document.getElementById('content-editor').value);}别的{alert("先选择一个文件");}},错误的);//document.getElementById('delete-file').addEventListener('click',function(){var actualFilePath = document.getElementById("actual-file").value;如果(实际文件路径){删除文件(实际文件路径);document.getElementById("实际文件").value = "";document.getElementById("content-editor").value = "";}别的{alert("先选择一个文件");}},错误的);document.getElementById('create-new-file').addEventListener('click',function(){var content = document.getElementById("content-editor").value;dialog.showSaveDialog(函数(文件名){如果(文件名 === 未定义){console.log("你没有保存文件");返回;}fs.writeFile(文件名,内容,功能(错误){如果(错误){alert("创建文件出错"+ err.message)}alert("文件保存成功");});});},错误的);函数文件读取完成(数据){我的数据 = 数据;//做你想做的}函数读取文件(文件路径,回调){fs.readFile(filepath, 'utf-8', function (err, data) {如果(错误){alert("读取文件出错:" + err.message);返回;}回调(数据);document.getElementById("content-editor").value = data;});}函数删除文件(文件路径){fs.exists(文件路径,函数(存在){如果(存在){//文件存在删除fs.unlink(文件路径,函数(错误){如果(错误){alert("更新文件出错"+ err.message);控制台日志(错误);返回;}});} 别的 {alert("此文件不存在,无法删除");}});}函数 saveChanges(文件路径,内容){fs.writeFile(文件路径,内容,功能(错误){如果(错误){alert("更新文件出错"+ err.message);控制台日志(错误);返回;}alert("文件保存成功");});}</脚本></div><!-- <script data-main="main" src="require.js"></script>--></身体></html>

完整的 main.js 文件

//console.log(require('fs'));console.log(require('module').globalPaths);常量 {电子} = 要求('电子');常量 {浏览器窗口} = 要求('电子')常量 {应用程序} = 要求('电子');//@show(应用程序)常量路径 = 要求('路径')//console.log(process.env.PATH);//(D:electron-v1.6.1-win32-x64
esourcesdefault_app.asrmain.js:325:5)//const BrowserWindow = require('browser-window')常量 url = 要求('url')var html = require('html-browserify');var fs = require('电子')//console.log(require('fs').existsSync);var remote = require('electron').remote;//var remote = require('remote');var dialog = require('electron').remote//dialog = require('electron').dialog//对话 =remote.require('对话')//var load_=require('./load_.js')//浏览 htmlvar through = require('through');var htmlclean = 需要('htmlclean');module.exports = 函数(文件,选项){选项 = 选项 ||{};options.htmlclean =typeof options.htmlclean !== '未定义'?options.htmlclean:真;变量缓冲区 = '';if (!/.(tpl|html)/.test(file)) {通过()返回;} 别的 {返回通过(函数(块){返回缓冲区 += chunk.toString();}, 功能() {var jst = buffer.toString();如果(options.htmlclean){//options.htmlclean 是真的if (typeof options.htmlclean === 'object') {//options.htmlclean 是 htmlclean 模块的选项对象jst = htmlclean(jst, options.htmlclean);} 别的 {//否则,使用默认选项清理jst = htmlclean(jst);}}var 编译 = 'module.exports = ';编译 += JSON.stringify(jst);编译 += ';
';this.queue(编译);返回 this.queue(null);});}}//requirejs.config({//默认从js/lib加载任何模块ID//baseUrl: 'js/lib',//除了,如果模块ID以app"开头,//从 js/app 目录加载它.路径//config 是相对于 baseUrl 的,并且//从不包含.js"扩展名,因为//路径配置可以是一个目录.//路径:{// 应用程序: '  '/

本文标题为:electron,browserify之后,fs.existsSync不是函数

基础教程推荐