Doubts about how Requirejs works

Asked

Viewed 704 times

1

I am making an application that will consume some Twitter data. I am using the library recommended by API developers, as my application is only to consume API I use only Javascript and jQuery, so I use the library Twitterjsclient.

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

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

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

When attaching this project to mine, at first it does not work because these functions do not belong to Javascript or jQuery, ie, Undefined.

What little I know about Requirejs, is that it carries files into another, enabling the use of it. However, even after using Requirejs (version ~2.1.14) I installed via Bower (bower install requirejs) the project does not work as it should.

The Error that gives is the following:

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 

Directory and file structure (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 show me a way to use the Twitterjsclient in my project?

  • You can put your folder structure and your oauth.js here?

  • I put it on, but I don’t know how to use Markdown. If you can tidy up!

  • Have you tried giving the complete path in require? the error you gave points to this: http://192.168.0.22:9000/oauth.js 404 (Not Found)

  • Turns out I don’t know how to use require well. I don’t really know if it’s trying to get the oauth.js file or the whole oauth directory. And as I use Yeaoman the root path of development diverges from the root path of production. I think it’s not cool to put complete path.

  • 'Cause require assumes the file is .js and then you don’t need to put the file extension. Test the full path in the browser to see if you find the file... type http://192.168.0.22:9000/lib/oauth.js

  • I changed the directory to "scripts/Twitterjsclient/node_modules/oauth/lib/oauth" and it worked. However, within this file there are other require for other files and also an undefined function called EXPORTS.

  • I noticed that require I installed via Bower always searches files from path "app/". I do not need to make any change on that, if that is possible?

Show 2 more comments

1 answer

1

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

Source: What is Require?

About how Node discovers modules, it always starts from the local folder to the node_modules project, then if it does not find search in the global Node installation.

  • Upon your explanation, you think I should migrate the node_modules directory that lies within the Twitterjsclient/ library to the node_modules directory of my app project/ !??

  • 1

    If you are using pure Node or Yeoman, Bower or other package manager you should not have to worry about this directory because it will be managed by such a tool. Problems usually arise when you copy the files by hand. https://www.npmjs.org/doc/files/npm-folders.html

  • The problem is that I need to download the twitter library and attach it to my project. This library is on Github and not npm. Soon I have to put 'in hand'. So in which folder should I put it and as a reference it in my project, if you need?

  • use Bower if your project is using, otherwise the way is to put in hand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.