Node.js in Electron + Angular 2 & TypeScript Application

I am developing an Electron application using Angular 2.
I followed this tutorial for the initial setup of the environment.
My setup is a little more complicated, but overall very similar. tsconfig.json :

 { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules", "app/node_modules", "dist" ] } 

systemjs.config.js exactly the same as in the tutorial, as well as index.html

One of my modules (which is located somewhere inside the app folder - app/*/*/*/module ) depends on node-ffi . Therefore, I added the necessary characters for typings.json :

 { "globalDependencies": { "core-js": "registry:dt/core-js", "jasmine": "registry:dt/jasmine", "node": "registry:dt/node", "ref": "registry:dt/ref", "ref-struct": "registry:dt/ref-struct", "ffi": "registry:dt/node-ffi" } } 

Now the module is trying to use ffi as follows:

 import { DynamicLibrary, Library, types } from 'ffi'; export class CppProxy { //Some code } 

which ultimately translates into:

 var ffi_1 = require('ffi'); //Utilize ffi_1 exported stuff 

According to this article, there is a clear way to search for the node.js module, and accordingly it should find the node-ffi module located in app/node_modules . However, it is not. It only looks in the app folder for ffi.js and obviously does not find it.
My first attempt to fix this is to add a ffi entry to the systemjs.config.js map systemjs.config.js , for example:

  var map = { 'app': '.', // 'dist', '@angular': 'node_modules/@angular', 'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', 'rxjs': 'node_modules/rxjs', 'node-binary': 'node_modules/systemjs-plugin-node-binary/node-binary.js', 'ffi': 'node_modules/ffi/lib/ffi.js' }; 

This helped to load ffi , but caused new problems. ffi itself depends on other modules:

 var ref = require('ref') var assert = require('assert') var debug = require('debug')('ffi:ffi') var Struct = require('ref-struct') var bindings = require('./bindings') 

So, now the application could not find these modules. I also tried adding some of them to the map, but again he just solved one level of dependency.

This seems wrong to me, require you should not search only in the app folder, and I don’t want to add all the dependencies recursively to display the systemjs.config.js section. What am I doing wrong?

Upd:
There is also a question dedicated to a rather similar problem, but it specifically asks about the use of require('remote') .
I am asking how to use the node.js module resolution mechanism when using System.js as the module loader.

0
source share
1 answer

Like Pace , mentioned in one of its comments, System.js overrides the Node.js require method and uses its own resolution mechanism. This is why the require method will not follow the Node.js search engine . However, there is a way to use the latter:

System.js stores the variable Node.js require in _nodeRequire . Thus, the way to load a module using the Node.js mechanism is to load it using

 var module = System._nodeRequire('./path/to/module/') 

Here is the discussion that helped me come up with this solution.

+2
source

All Articles