Electron + Angular2 - import modules

I created an application with electron and Angular2 (Angular -CLI). Unfortunately, I cannot import node modules such as 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 already found out that I can solve the problem if I add the following lines to my html

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

and use for import

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

But with this solution, I cannot use typing.

I know this because Angular2 requires modules. Angular2 has its own method. Thus, it works with nodeRequire.

For packaging, I use webpack with AngularCLI. And my tsconfig.json looks like this

{
  "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 can I import node modules into electron project + Angular2 with type?

+4

Source: https://habr.com/ru/post/1664441/


All Articles