1

I am a little confused about multiple ways of importing files in Javascript (script tag, RequireJS, Component - if it is another way to include files). In my project, I am using AMD with RequireJS.
Now, I want to use superagent library.
I donwnloaded superagent.js file, and I included it in some test pages using script tag. Then, I can use a require function (defined in superagent.js), and I definitely have access to superagent functions, if I write:

var request = require('superagent');

in my JS file.
Is there a way to use Superagent library in my poject, which is based on RequireJS? I hope to still use:

var request = require('superagent');

, but clearly it is not working.

marcos82
  • 997
  • 1
  • 8
  • 11
  • Works for me (I did a quick check based on an [example template project](https://github.com/volojs/create-template)). Can you verify that the `superagent.js` resource is correctly loaded and doesn't 404? Superagent is AMD-ready so if the path is correct it will be picked up automatically. Can you be more specific than *"clearly it is not working"*? – kryger Dec 19 '14 at 15:56
  • I don't have 404, and I can see some code executed dureing _require_ function call, if I set some breakpoint in the code. But, if I use that line of code, _request_ is undefined. Where did you get source code of superagent.js? I got it using `component install visionmedia/superagent` command and "compiling" it (I don't know more details about compilation, because I didn't make it on myself). – marcos82 Dec 19 '14 at 17:11

3 Answers3

0

You can try this:

require(["superagent"], function(superagent){
    console.log(superagent)
    // there your code 
})
kryger
  • 12,906
  • 8
  • 44
  • 65
Yurii Kovalenko
  • 419
  • 3
  • 10
  • I forgot to tell that I am using the CommonJS syntax ([http://requirejs.org/docs/commonjs.html#exports]), and I'd like to use it. Anyway, I tried your suggestion, but superagent is yet undefined... I had a look at the code, and I am not sure that it can be used in AMD env, but I am not so expert. – marcos82 Dec 19 '14 at 15:39
  • Do you try to add this library to "shim" in Requirejs config ?http://requirejs.org/docs/api.html#config-shim – Yurii Kovalenko Dec 19 '14 at 15:46
  • I didn't try, because this library defines again "require" function, which I use to include other AMD libraries with RequireJS. I found another solution, as explained in answer. – marcos82 Dec 19 '14 at 15:49
0

Eventually, I found a sort of workaround.
I used "component" version of the library (even if I have no precise idea of what is component, so any help in this sense is appreciated), and I envelope it in a classical define RequireJS, without explicitly using require, as args passed to defining function (I don't need a require function, because it is defined in the library itself).

define(function () {
.... [all library content]...
return require("superagent");

});

I just added first line, and return before last line of library (which was simply require("superagent")).
It seems to work, even if I remain confused about multiple ways to include JS files, and including JS libraries is nowaday every time an adventure...

marcos82
  • 997
  • 1
  • 8
  • 11
  • This [answer](http://stackoverflow.com/a/21023168/1906307) should clarify the differences between the multiple ways to call `require`. – Louis Dec 19 '14 at 16:46
0

This works for me : Create a require.config.js file call it say bootstrap.js, within that choose a simple name for superagent. It'll look something like:

requirejs.config({
   map: {},
  paths: {
    'superagent': 'libs/superagent/superagent',
  },
  shim: {},
  packages: []
});

require(['js/main/main']);

In your index.html point require.js to bootsrap.js e.g.

Now in your modules use

define(
    ['superagent'],
    function(request) {
    }
);
Tarka
  • 4,041
  • 2
  • 22
  • 33
Davet
  • 334
  • 1
  • 3
  • 15
  • Sorry your index.html file should have within a script tag ' data-main="bootstrap" src="path-to-require.js" ' – Davet Apr 15 '15 at 16:26