0

In any service of ionic3 app

import * as jsnx from 'jsnetworkx';

the ouput error is this

Uncaught (in promise): Error: Cannot find module "lodash/lang/isPlainObject" Error: Cannot find module "lodash/lang/isPlainObject" at webpackMissingModule

I tried to declare it this way

import jsnx = require('jsnetworkx');

and the error is this

 Uncaught (in promise): ReferenceError: jsnx is not defined ReferenceError: jsnx is not defined at

both packages are installed

...,
"jsnetworkx": "^0.3.4",
"lodash": "^4.17.4",
...

If any know how to do that works in angular 4 o ionic?

The library with node work fine.

alehn96
  • 1,363
  • 1
  • 13
  • 23
  • Maybe you can try this approach https://hackernoon.com/how-to-use-javascript-libraries-in-angular-2-apps-ff274ba601af to import the library, as long as it does not seem to be exported in the ES6 module format. – Christian Benseler Nov 01 '17 at 16:30
  • The problem is that in ionic not exists the .angular-cli.json file and I not can put in scripts:[] the jsnetworkx library – alehn96 Nov 01 '17 at 16:37

1 Answers1

2

I was able to get this working by installing d3 v3 (dependency of jsnetworkx) alongside jsnetworkx

npm install --save d3@^3.0.0
npm install --save jsnetworkx

Then loading the d3 script in angular-cli.json

// angular-cli.json
scripts: [
    "../node_modules/d3/d3.min.js"
]

Then importing jsnetworkx into the component

// component.ts
import * as jsnx from 'jsnetworkx';

Now you can use it within that component

// component.ts
ngOnInit(){
    // basic jsnetworkx example
    let G = new jsnx.Graph();

    G.addWeightedEdgesFrom([[2,3,10]]);
    G.addStar([3,4,5,6], {weight: 5});
    G.addStar([2,1,0,-1], {weight: 3});

    jsnx.draw(G, {
        element: '#canvas',
        weighted: true,
        edgeStyle: {
            'stroke-width': 10
        }
    });
}

// component.html
<div id="canvas"></div>
LLai
  • 13,128
  • 3
  • 41
  • 45
  • same error Uncaught (in promise): Error: Cannot find module "lodash/lang/isPlainObject" Error: Cannot find module "lodash/lang/isPlainObject" at webpackMissingModule . – alehn96 Nov 01 '17 at 17:18
  • @alehn96 can you post the component jsnetworkx code that you are using? – LLai Nov 01 '17 at 17:20
  • I installed d3, but in ionic not exists .angular-cli.json, when run ionic serve, the same error with module lodash – alehn96 Nov 01 '17 at 17:20
  • @alehn96 you are not using angular cli? regardless, you'll need to load the appropriate libraries. How did you install jsnetworkx? with npm? – LLai Nov 01 '17 at 17:30
  • Like you with npm install, the project ionic doesnt have angular-cli – alehn96 Nov 01 '17 at 17:33
  • @alehn96 looks like jsnetworkx cannot find lodash. Try seeing if lodash exists at `/node_modules/jsnetworkx/node_modules/lodash`. Perhaps your npm install did not install jsnetworkx correctly – LLai Nov 01 '17 at 17:44
  • Thank you very much, I resolved downgrade version of lodash of my app to version of lodash of jsnetworkx – alehn96 Nov 01 '17 at 18:04