goJS not loading in Electron app

I’ve got a problem with goJS module not somehow getting its declaration working in an Angular2 app when it is loaded in Electron. What is weird is that it loads, and resolves definitions fine when I run the exact html/js code as a simple website.

my local.component.ts code has a simple:

	declare var go:any;
	... ...
	...  ...
			var newDiagram = go.Model.fromJson(modelData);

here’s the bit of code to access that module in my webpack.config –

    module: {
        loaders: [
            // Only apply on gojs/release/go-debug.js
            {include: require.resolve('gojs/release/go.js'),  loader: 'exports?go'},
			... .... 
    resolve: {
        alias: {
             // require('go') will do require('gojs/release/go-debug.js')
            go: 'gojs/release/go.js'

I’ve been beating my head against a wall for 3 days trying to understand why “go” resolves and works when I load it off of localhost:3000, but inside the electron app the module.export = go is failing because it says 'go is undefined'.

I’ve tried everything from using a script tag include in the HTML (index) directly, to including the module at the main app module file, to putting it in the local component I need it in… same result in each case-- it works to resolve the definition when I load the page in regular browser… but fails when I try to run it inside electron.

I’ve looked at the one sample included in the GoJS samples directory that talks about electron. It is using a script src= tag to pull in goJS… I tried that method too… again, it works when I do that to launch it as a website (ie. localhost:3000) but fails when I load it in electron.

I don’t know what might be wrong. I didn’t have any problems with that when I was developing that sample. But that was a while ago, and maybe something has changed.

In my recent testing with TypeScript I have been using import rather than require:

import * as go from "go";

I don’t know whether that would matter for you.

The solution you gave–

  import * as go from "go";

has the same result as every other things I attempted- it works on regular web browser, but doesn’t work on Electron.

After trying a lot of things I found the only way I could get it to work was to load the gojs library in a script tag, and surround it by code to check for module.export values existing and storing it in a local variable, as follows:

   if (typeof module !== 'undefined') {
     window.__tempModuleExports__ = module.exports;
     module.exports = false;
 <script src='https://cdnjs.cloudflare.com/ajax/libs/gojs/1.6.17/go-debug.js'></script>
   if (window.__tempModuleExports__) {
     module.exports = window.__tempModuleExports__;
     window.__tempModuleExports__ = null;
     delete window.__tempModuleExports__;

I got the idea from a blog post where someone was having the same problem with jQuery not loading in Electron. It isn’t a pretty solution but it works.

Really? That’s pretty wild. I’m glad you found a work-around and posted it here for others to use. Thanks.