Questions about how it works or RequireJS

1

I'm making an application that will consume some Twitter data. I am using the library recommended by the API developers, as my application is just for consuming API I only use javascript and jQuery , then I use the TwitterJSClient library.

In this library, at the beginning of the file /TwitterJSClient/lib/Twitter.js it has the following code:

var OAuth = require('oauth').OAuth;
var qs = require('qs');

After analyzing your folder and file structure, I saw that it has some directories named oauth and qs .

When attaching this project to mine, the first one does not work because these functions do not belong to JavaScript and jQuery, ie undefined .

The little I know about RequireJS is that it loads files into another, making it possible to use them. However, even after using the RequireJS (version ~ 2.1.14) that I installed via Bower (% with%) the project does not work as it should.

The error you give is as follows:

Uncaught Error: Module name "oauth" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded 

I changed the project code to:

var OAuth = require(['oauth']).OAuth;
var qs = require(['qs']);

And the result changes to:

GET http://192.168.0.22:9000/oauth.js 404 (Not Found)
Uncaught Error: Script error for: oauth
http://requirejs.org/docs/errors.html#scripterror
GET http://192.168.0.22:9000/qs.js 404 (Not Found)
Uncaught Error: Script error for: qs
http://requirejs.org/docs/errors.html#scripterror 

Structure of directories and files (partial):

Project/
  app/
    images/
    scripts/
      TwitterJSClient/
        lib/
          Twitter.js
        node_modules/
          .bin/
          jasmine-node/
          oauth/
            examples/
            lib/
              _ultils.js
              oauth.js
              oauth2.js
              sha1.js
            tests/
            index.js
          qs/
            test/
            index.js
        test/
        index.js
      main.js
    styles/
    index.html
  bower_components/
    requirejs/
      require.js
  node_modules/
  test/

Can someone tell me a way to use TwitterJSClient in my project?

    
asked by anonymous 30.06.2014 / 05:19

1 answer

1

Require is a builtin function of the Node that serves to include modules that are in other Js files. It works by reading the file passed in parameter, executing and then returning the Exports

Source: What is Require?

On how the node discovers the modules, it always starts from the local folder to the node_modules project, then does not find the global node's lookup.

    
01.07.2014 / 16:10