我对Angular、Javascript等话题非常陌生。
我尝试编写一个(TypeScript) Angular2-Electron应用程序,它可以访问文件系统。每个人都说需要"fs“模块,一切都很好,但这对我不起作用……。
如果我这样做:var fs = require('fs');
我可以看到我的应用程序尝试从我的应用程序根文件夹加载"fs“模块:..myapp/dist/fs net::ERR_FILE_NOT_FOUND
我的所有其他外部模块都在index.html中被引用:
<!-- build:js app/scripts/combined.js -->
<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="../node_modules/systemjs/dist/system.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/http.js"></script>
<script src="../node_modules/angular2/bundles/router.js"></script>
<script src="../node_modules/rxjs/bundles/Rx.js"></script>
<script src="../node_modules/bootstrap/dist/js/bootstrap.js"></script>
<script src="../node_modules/pdfjs-dist/build/pdf.combined.js"></script>
<script src="boot.js" type="text/javascript"></script>
<!-- endbuild -->
因此我认为它们是可以找到的,但是"fs“属于node.js,它存在于电子中?还是我在思想上犯了一些大错误?
非常感谢,
克里斯
发布于 2016-04-17 00:55:25
问题似乎是我在Angular应用程序中使用了SystemJS。SystemJS尝试从我自己的应用程序加载模块。
我现在已经添加了这个,所以我的index.html似乎可以工作了:
<script>
if (require) {
window.$ = window.jQuery = require('./app/assets/js/jquery.min.js');
window.fs = require('fs');
window.path = require('path');
}
</script>发布于 2016-12-28 21:25:33
我管理着一个github project,它涵盖的不仅仅是Angular 2和electron (它还涉及到本地库)。
我遇到了许多与您的问题类似的问题,特别是从electron应用程序访问node.js模块的问题。
也许用它作为起点是值得的,而不是试图从头开始写所有的东西。
至于您的问题,您应该使用System._nodeRequire('fs')而不是require('fs'),因为SystemJS查找机制与node的查找机制有点不同。
发布于 2016-12-28 04:23:40
让我引用meltedspark's answer来回答一个类似的问题:
System.js覆盖了Node.js的require方法,并使用它自己的解析机制。
以下是我对这个问题的解决方案:
Node.js模块创建重新导出:// scripts/node- re-exports /fs.ts声明const System: any;const fs = System._nodeRequire('fs');export = fs;// scripts/node-re-exports/electron.ts声明const System: any;const fs.ts=System._nodeRequire(‘fs.ts’);export =电子;
systemjs.config.js知道在哪里搜索这些重新导出。map:{ ... //重新导出Node.js模块。fs:‘编译/节点-重新导出/fs.js’,电子:‘编译/节点-重新导出/Electron.js’}
从‘@Angular/core’导入{ Component };从‘电子’导入{剪贴板};从‘fs’导入{ existsSync };@Component({选择器:‘my-<h1>Hello {{name}}</h1>’,模板:<h1>Hello {{name}}</h1>,})导出类名称{ AppComponent = 'Angular';const text (){const text= existsSync("c:\boot.txt")?"exists“:”不存在“;clipboard.write({ text: text });};}
https://stackoverflow.com/questions/36663454
复制相似问题