Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load commonjs module from requirejs, document not describe clearly. #1037

Open
guileen opened this issue Feb 19, 2014 · 5 comments
Open
Labels

Comments

@guileen
Copy link

guileen commented Feb 19, 2014

I follow http://requirejs.org/docs/api.html#packages to learn how RequireJs load CommonJS modules, but I can't get it.

I have below file.

foo/foo.js
main.js
require.js

foo/foo.js with content:

exports.bar = '1234';
exports.xyz = function noop() {};

main.js:

requirejs.config({
    packages: [{name:'foo', main: 'foo'}]
});

require(['foo'], function(foo) {
   console.log(foo.bar);  // << error here
});

But it said can't read bar of undefined

@jrburke
Copy link
Member

jrburke commented Feb 19, 2014

packages config is only for dealing with code that is laid out using a package directory structure like the Packages spec mentioned on the CommonJS site, it is not about understanding how to load a CommonJS-formatted module. If you want that, then cajon might be a better loader option.

Closing as I think the cajon loader is a better fit for the described use case.

@jrburke jrburke closed this as completed Feb 19, 2014
@guileen
Copy link
Author

guileen commented Feb 19, 2014

The document described folder structure, but it never said what the cart/main.js content should look like.

it is

export.foo = 'bar'  // I think this is a CommonJS module define

or

define(function(require, exports) {
//...
})

RequireJS supports loading modules that are in a CommonJS Packages directory structure, but some additional configuration needs to be specified for it to work.

But what a RequireJS supported CommonJS Packages's main javascript file content look like, docs never metioned.

@jrburke jrburke added this to the 2.1.12 milestone Feb 19, 2014
@jrburke
Copy link
Member

jrburke commented Feb 19, 2014

That makes sense, will reopen to track a doc fix to explain more.

@jrburke jrburke reopened this Feb 19, 2014
@jrburke jrburke modified the milestones: 2.1.13, 2.1.12, 2.1.14 May 22, 2014
@jrburke jrburke modified the milestones: 2.1.15, 2.1.14 Jun 2, 2014
@jrburke jrburke added the docs label Sep 7, 2014
@jrburke jrburke removed this from the 2.1.15 milestone Sep 7, 2014
@demonfiddler
Copy link

Any intention to address this? Like reporter @guileen I've read all the documentation and I'm still baffled.

I'm trying to use RequireJS to load the Node modules 'events' and 'util' but they just throw the error 'module is not defined'.

@ScottFreeCode
Copy link

This seems pretty well covered under http://www.requirejs.org/docs/commonjs.html, which is linked from the sidebar ("CommonJS Notes") and from the package config API section:

IMPORTANT NOTES

  • While the packages can have the CommonJS directory layout, the modules themselves should be in a module format that RequireJS can understand. Exception to the rule: if you are using the r.js Node adapter, the modules can be in the traditional CommonJS module format. You can use the CommonJS converter tool if you need to convert traditional CommonJS modules into the async module format that RequireJS uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants