Fork me on GitHub

Installing uRequire


uRequire has a separate [command line invoker]( that is installed globally:

    `npm install urequire-cli -g`

You 'll also need a local dependency of 'urequire' package in your project (i.e npm install urequire --save-dev).

a) urequire-cli just loads the local urequire package.

b) For your modules-to-become-UMD with the UMD template (when those are running on node, not used in UMDplain template)

So do a npm install urequire or add to package.json.

This actually gives your UMD modules a proxy to node's native require, allowing proper paths resolution, the asynchronous version of require, requirejs loader plugins execution, mappings and better debugging information when things go wrong.

Basic Usage

Assuming you have your AMD/node modules in a structure like this


The src/ directory is said to be your 'bundle root', in urequire terms. It's what you would set baseUrl to in requirejs, if your modules were in pure AMD format. All absolute dependencies (those not starting with ./, ../ or /) would be relative to this bundle root, eg 'Application' or 'views/PersonView'. Every UMD file is aware of its location in the bundle and uses it in various ways, such as resolving paths, looking for 'requirejs.config.json', resolving baseUrl/paths & webRootMap etc.

Now say your views/PersonView.js is

define(['models/PersonModel'], function(PersonModel) {
  var helper = require('helpers/helper.js');
  //do stuff with PersonModel & helper
  return {the:'PersonViewModule'}

and similarly for the others. Note that even this tiny valid-looking module is authored in the 'relaxed' Universal Module Format, that would NOT as plain AMD.

Remember that other modules in the same bundle can be authored as plain nodejs or AMD modules. For example 'models/PersonModel.js' can be :

var helper = require('helpers/helper.js');
var data = require('datastore/data.js');
// do stuff with data & helper
module.exports = {the:'PersonModelModule'}

To convert your modules to uRequire UMD you 'll execute:

urequire UMD src -o build

This will place the translated UMD files into the build directory. The generated files will look similar to this:

  // Generated by urequire v0.0.9
  (function (root, factory) {
      if (typeof exports === 'object') {
          var nodeRequire = require('urequire').makeNodeRequire('views/PersonView.js', __dirname, '..');
          module.exports = factory(nodeRequire, nodeRequire('../models/PersonModel'));
      } else if (typeof define === 'function' && define.amd) {
          define(['require', '../models/PersonModel', '../helpers/helper.js'], factory);
  })(this, function (require, PersonModel) {
      var helper = require('../helpers/helper.js');
      return {the:'PersonViewModule'};

Your bundle files are ready to be deployed to Web/RequireJS and to node (by having 'urequire' locally installed via npm).


urequire UMDplain src -o build

if you dont want a simpler UMD template that doesn't have 'urequire' dependency on your project for nodejs execution.

Check Using uRequire for usage details.