For Install r.js
npm install -g requirejs
If you’ve been frustrated to hell using RequireJS on Windows, and the r.js optimizerin particular, you might have witnessed things like WSH errors or your editor opening the
r.js file. I’ve seen a few frustrated people post bug reports on GitHub, so I decided to post a few solutions to the issue.
Before we begin, let me note one thing. The optimizer works just fine on Windows. The documented way of running the optimizer is to use
r.js.cmd command instead of just
r.js, and if you stick to that recommendation you shouldn’t have any issues. In the following paragraphs, I will describe a few ways to invoke
r.jswithout involving the (slightly) awkward
When you install the requirejs package using npm, it will install two files in
%HOMEDRIVE%%HOMEPATH%\AppData\Roaming\npm directory. One is called
r.jsand another is
r.js.cmd. The problem is the former. Although it has a
.jsextension, it’s actually a shell script (for the uninitiated, it’s a UNIX/Linux thing), and not meant to be run with NodeJS at all.
r.js file allows you to run the
r.js.cmd file without the extension. Use the following command to delete it:
Be sure to leave the
.cmd file intact.
If you haven’t used
DOSKEY before, you should give it a try. An article by Dan Fabulich gives you a condensed version of how to set up permanent DOSKEY macros that will load each time you open command prompt.
To set up a macro for
r.js use the following command:
DOSKEY r.js=r.js.cmd $*
This will override the
r.js file in the
%HOMEDRIVE%%HOMEPATH%\AppData\Roaming\npm directory, and allow you to use
r.js without the
.cmd extension. Be sure to save the macro as per Dan’s instructions.
Use a build system
This may be quite obvious, but you can use a proper build system that would call the RequireJS optimizer on your behalf. This does away with the poblem of how to invoke
r.js, and also gives you other goodies commonly available in such systems. Try volo or Grunt.
volo deserves a special recommendation since it’s maintained by James Burke, author of RequireJS, and comes with a great project template that sets things up for requiring and building, and the ability to convert modules to AMD when installing from online repositories or other locations.