Electron + Angular2 - import modules

340 Views Asked by At

I created an application with electron and Angular2 (Angular-CLI). Unfortunately, I can't import node modules like electron.

import {remote} from "electron";

Whenever I try to import node modules and use them I get the following errors:

Uncaught TypeError: fs.readFileSync is not a function

I've already found out that I can solve the problem if I add the following lines into my html

<script>
   window.nodeRequire = require;
   delete window.require;
   delete window.exports;
   delete window.module;
</script>

and use for the import

const remote = nodeRequire("electron").remote;

But with this solution I can't use typings.

I know it's due to how angular2 require modules. Angular2 has it's own require method. Thus, it works with nodeRequire.

For packaging I use webpack with AngularCLI. And my tsconfig.json looks like the following

{
  "compilerOptions": {
     "baseUrl": "",
     "declaration": false,
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "lib": [
       "es6",
       "dom"
     ],
     "mapRoot": "./",
     "module": "commonjs",
     "moduleResolution": "node",
     "outDir": "../dist/out-tsc",
     "sourceMap": true,
     "target": "es5",
     "typeRoots": [
       "../node_modules/@types"
     ]
  }
}

So, is there any approach or solution how I can import node modules in an electron + angular2 project with typings?

0

There are 0 best solutions below