Ionic3 & Electron : fs.existsSync is not a function

#1

Hello,

I try to combine Ionic3 with Electron to develop an “universal” application.

I followed this tutorial and try to make a provider to access electrons apis but I have an error at launch :

Concerning electron integration, I added :

src/app/app.module.ts

import { ElectronProvider }                         from '../providers/electron/electron';

@NgModule({
  providers: [
    ElectronProvider
  ]
})

src/providers/electron/electron.js

import { Injectable } from '@angular/core';
import * as electron  from 'electron';

/*
  Generated class for the ElectronProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class ElectronProvider {
  currentZoom: number = 0;

  constructor() {
  }

  /* For testing...*/
  zoomIn(){
    electron.webFrame.setZoomLevel(++this.currentZoom);
  }
  zoomOut(){
    electron.webFrame.setZoomLevel(--this.currentZoom);
  }
}

Where am I wrong?

Thanks in advance

#2

It’s solved for Electron : I have to add a function in webpack.config :

externals: [
   (function () {
       var IGNORES = ["fs","child_process","electron","path","assert","cluster","crypto","dns","domain","events","http","https","net","os","process","punycode","querystring","readline","repl","stream","string_decoder","tls","tty","dgram","url","util","v8","vm","zlib"];
       return function (context, request, callback) {
           if (IGNORES.indexOf(request) >= 0) {
               return callback(null, "require('" + request + "')");
           }
           return callback();
       };
   })()
 ]

It works fine inside Electron, but not anymore in navigator or Android… but it’s another problem (error “require is not a function” from the new function in external)