4

My goal is to take Angular 2's 5 Minute Quickstart and turn it into an Electron app. Which I can do by adding "electron-prebuilt".

angular running

What I can't do is utilize any of Electron's extended captabilites - for example writing to the file system.

I can build and run "Electrogram", but when I try to utilize some of the basic functionality contained within it I get an error in the console of my Electron application:

Altered app.component.ts

import { Component } from '@angular/core';
import { remote, ipcRenderer } from 'electron';
import { writeFile } from 'fs';

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1><button (click)="doSomething()">Do Something</button>'
})
export class AppComponent {

  doSomething() {
    writeFile("c:\temp\doSomething.txt", "some data");
  }
}

Error in DevTools Console

App not working

How can I configure the app to look for the Electron modules in their correct locations?

4
  • I just installed Electrogram and added the following line to the constructor of class App in app.ts: "writeFile('./aaa.txt', 'bbb');" (without quotes...) and the file is written in the root of the project... What did you change to Electrogram? Commented Aug 27, 2016 at 11:58
  • It's not that I can't get Electrogram to work, it's that I can't get electron to import into the 5 minute quickstart app and have it find the FS module. Commented Aug 28, 2016 at 23:20
  • 1
    I just started playing around with ts/js and stumbled over incompatible module loaders, client vs server, import vs require, etc. Too confusing for a simple C#, Python, Php, Android developer like me... Instead of trying to add electron to Quickstart, I added the Quickstart app folder into a working electron application (Electrogram). That worked for me... Commented Aug 30, 2016 at 5:23
  • any solution found by now? Commented Jan 25, 2017 at 17:22

1 Answer 1

2

Due to differences between module loaders I've run into the same issue. I use different solutions based on how the app is being build.

Webpack:

Webpack bundles all modules and dependencies into a single resulting app.js (depending on config). In this case import { readFile } from 'fs', works fine. The demo app Electrogram is using webpack.

To me this is the most convenient and fastest way of building the app and the most performant at runtime.

SystemJs

In case SystemJS is used as module loader I have to use the following to load the FS module:

let remote = require('electron').remote;
let fs = remote.require('fs');
...
let data = fs.readFileSync(this.dbFilename);

Hope this helps.

(PS. Do I understand all of this? No, I don't...)

Sign up to request clarification or add additional context in comments.

2 Comments

Interestingly, this only happens for 'fs' module but there is no problem for 'path' - although both are require'd() in the same module file...
@JindrichVavruska As I said... I have no clue what is happening under the covers...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.