Tuesday, July 19, 2011

RequireJS node adapter now in npm

[Update August 17, 2011: This post is out of date, see the 0.26.0 release for a better way to use RequireJS with npm.]

[Update July 21, 2011
: GitHub users arlolra and JasonGiedymin clued me in to setting up r.js as a bin file that can be installed globally, making it much easier to use. The instructions below have been updated to reflect the changes.]

As asked for on the requirejs list, the RequireJS Node adapter + optimizer is available via npm now:

npm install -g requirejs-r

This will install the r.js file as a global bin/executable that is available for any of your Node projects.

To run main.js via RequireJS in Node:

r.js main.js

To optimize a web project using RequireJS, assuming app.build.js contains your optimization profile:

r.js -o app.build.js

"requirejs-r" is a bit of a funny package name, but it comes out of the requirements to use a directory for publishing to NPM, and the r.js name. If it seems useful, I may publish other RequireJS-friendly loader plugins under the "requirejs-" prefix. I may even push require.js to npm, but I am not sure it makes sense yet. If you think it would be useful to you, feel free to leave a comment on this issue.

I'll update the requirejs.org docs if this installation option seems to work out for people.

4 comments:

Anonymous said...

rockin' - thanks james!

Jason Giedymin said...

Thanks James!! Wish I had more time to lend a hand!

Yesh said...

Do we need a separate RequireJS adapter to use require JS in other JS environments like spidermonkey JS runtime?

James Burke said...

Yesh: Right now the requirejs adapter works in Node and Rhino. If you need it to work in Spidermonkey, please feel free to open an issue here:

https://github.com/jrburke/r.js/issues

and explain what JS environment you would like see supported. For spidermonkey in particular, describing how spidermonkey is used. As I recall, it can be embedded in other wrappers that may provide some of the IO routines, so to get an effective adapter, more detail on how the environment executes is helpful.