简体   繁体   中英

Importing node modules with electron and Systemjs

I just wondered if it is possible to make systemjs use require("remote").require("nodemodule"), if system js can't find the module in its own registry?

I think something like this mechanism is already working when using electron with typescript and commonjs modules...

Has someone already solved that struggle?

Finally after some time I found a working solution:

var node_modules = ["child_process","fs"];
var fetch = System.fetch;
window.remote=require("remote");
System.fetch = function () {
    var promise= fetch.apply(System,arguments);
    return promise.then(function (js) {
        for(var m of node_modules){
            var requireExpression = 'require("'+m+'");';
            var remoteRequire = 'remote.require("'+m+'");'
            js=js.replace(requireExpression,remoteRequire);
        }
        return js;
    });
}
System.import("aurelia-bootstrapper");

Just add all imported node_modules to the array and things are fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM