Mopparthy Ravindranath
Mopparthy Ravindranath

Reputation: 3308

using a require js based module with relative paths

I am trying to use a library called g.js (https://github.com/nodebox/g.js), which is a graphics package. The script file "g.js" is the entry point file which is "requiring" other files using the syntax:

// Utility functions
g.bezier = require('./util/bezier');
g.color = require('./util/color');
g.geo = require('./util/geo');
g.math = require('./util/math');
g.random = require('./util/random');
g.svg = require('./util/svg');

My question is... how to use this set of library files within my html / javascript files?

For my part, I tried the following:

Loaded require.js in my html file:

<script src="scripts/require.js" data-main="scripts/main"></script>

My main.js looks like this:

require.config({
baseUrl : "scripts",
paths : {
  'jquery' : 'jquery-1.11.1',
  'lodash' : 'libs/lodash',
  'module_g_test' : 'module_g_test'
},
packages: [
  {
    name: "g",
    location: "g.js-master/src",
    main: "g"
  }
]
});

require(['jquery', 'module_g_test'], function($, g_test) {
  $(function() {
    console.log("basic stuff ready!");
    g_test();
  });
});

Finally, I am trying to use the graphics libraries in my module script file

define(['g'], function(g) {

  var testModule = function() {
    var x = g.Path.prototype;
    console.log(x);
  };

  return testModule;
});
~

No matter what I do, I am unable to load the internal javascript files accessed by g.js. I get a requirejs error:

Error Module name "util/bezier" has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded

Any help highly appreciated.

Upvotes: 0

Views: 481

Answers (1)

matys84pl
matys84pl

Reputation: 8334

This is a library written in commonjs style that uses browserify. Clone the repository and:

  1. run npm install
  2. run npm run browserify
  3. link the script from build/g.js in your site

The distribution version has an UMD implemented so it should work with RequireJS.

Upvotes: 1

Related Questions