Command-line tool for running AMD projects and invoking the build optimizer in Node.js and Java/Rhino environments.
Execute AMD-based applications from the command line in Node.js or Java/Rhino environments.
# Node.js execution
node r.js <main-script> [arguments...]
# Java/Rhino execution
java -classpath rhino.jar org.mozilla.javascript.tools.shell.Main r.js <main-script> [arguments...]
# With Rhino debugger
java -classpath rhino.jar org.mozilla.javascript.tools.debugger.Main r.js <main-script>Usage Examples:
# Run AMD application
node r.js main.js
# Run with arguments
node r.js app.js --env=production --port=3000
# Java/Rhino (Linux/Mac)
java -classpath lib/rhino/js.jar org.mozilla.javascript.tools.shell.Main r.js main.js
# Java/Rhino (Windows)
java -classpath lib\rhino\js.jar org.mozilla.javascript.tools.shell.Main r.js main.js
# Debug mode
java -classpath lib/rhino/js.jar org.mozilla.javascript.tools.debugger.Main r.js main.jsRun the RequireJS optimizer to combine and minify modules for production deployment.
# Basic build optimization
node r.js -o <build-config-file>
node r.js -o <build-config-object>
# Java/Rhino build (with Closure Compiler)
java -classpath rhino.jar:closure-compiler.jar org.mozilla.javascript.tools.shell.Main r.js -o <build-config>Usage Examples:
# Build with config file
node r.js -o build.js
# Build with inline configuration
node r.js -o baseUrl=. name=main out=built.js
# Build with app directory
node r.js -o appDir=src baseUrl=scripts dir=build
# Java build with all features
java -classpath lib/rhino/js.jar:lib/closure/compiler.jar \
org.mozilla.javascript.tools.shell.Main r.js -o build.jsGet version information for r.js and RequireJS.
# Get version information
node r.js -v
java -classpath rhino.jar org.mozilla.javascript.tools.shell.Main r.js -vUsage Examples:
# Check versions
$ node r.js -v
r.js: 1.0.8, RequireJS: 1.0.8, UglifyJS: 1.3.4
# In scripts
VERSION=$(node r.js -v)
echo "Using RequireJS version: $VERSION"Convert CommonJS modules to AMD format with define() wrappers.
# Convert CommonJS to AMD
node r.js -convert <input-directory> <output-directory>
# Java/Rhino conversion
java -classpath rhino.jar org.mozilla.javascript.tools.shell.Main r.js -convert <input-dir> <output-dir>Usage Examples:
# Convert entire directory
node r.js -convert src/commonjs-modules build/amd-modules
# Convert specific project
node r.js -convert node_modules/my-commonjs-lib build/converted/my-lib
# Check conversion results
node r.js -convert legacy-code amd-code
ls -la amd-code/The r.js script automatically detects and configures the appropriate runtime environment.
// Environment-specific globals available in r.js
interface EnvironmentGlobals {
// Node.js environment
process?: NodeJS.Process;
require?: NodeRequire;
module?: NodeModule;
exports?: any;
Buffer?: typeof Buffer;
// Rhino environment
Packages?: any;
java?: any;
print?: Function;
readFile?: Function;
// Common
console: Console;
}
// Environment detection
var env: "node" | "rhino" = typeof process !== "undefined" ? "node" : "rhino";Usage Examples:
// In your AMD modules (when run via r.js)
define(function() {
if (typeof process !== 'undefined') {
// Node.js specific code
var fs = require('fs');
return {
readConfig: function() {
return JSON.parse(fs.readFileSync('config.json', 'utf8'));
}
};
} else {
// Rhino specific code
return {
readConfig: function() {
return JSON.parse(readFile('config.json'));
}
};
}
});Access command-line arguments passed to AMD applications.
// Node.js environment
process.argv: string[]; // ['node', 'r.js', 'main.js', ...args]
// Rhino environment
arguments: string[]; // ['main.js', ...args]
// Cross-platform argument access
var args = typeof process !== 'undefined' ?
process.argv.slice(3) : // Skip 'node', 'r.js', 'main.js'
Array.prototype.slice.call(arguments, 1); // Skip 'main.js'Usage Examples:
# Pass arguments to AMD application
node r.js app.js --config=prod.json --verbose --port=8080// In app.js - access arguments
define(function() {
var args = typeof process !== 'undefined' ?
process.argv.slice(3) :
Array.prototype.slice.call(arguments, 1);
console.log('Arguments:', args);
// Output: ['--config=prod.json', '--verbose', '--port=8080']
// Parse arguments
var config = {
verbose: args.indexOf('--verbose') !== -1,
port: 3000,
configFile: 'default.json'
};
args.forEach(function(arg) {
if (arg.startsWith('--port=')) {
config.port = parseInt(arg.split('=')[1]);
}
if (arg.startsWith('--config=')) {
config.configFile = arg.split('=')[1];
}
});
return {
start: function() {
console.log('Starting with config:', config);
}
};
});Access file system operations in both Node.js and Rhino environments.
// Node.js file operations (available via r.js)
interface NodeFileSystem {
readFileSync(path: string, encoding: string): string;
writeFileSync(path: string, data: string, encoding?: string): void;
existsSync(path: string): boolean;
readdirSync(path: string): string[];
statSync(path: string): Stats;
}
// Rhino file operations (available via r.js)
interface RhinoFileSystem {
readFile(path: string): string;
// File existence check
new java.io.File(path: string).exists(): boolean;
// Directory listing
new java.io.File(path: string).listFiles(): java.io.File[];
}Usage Examples:
// Cross-platform file reading
define(function() {
function readTextFile(path) {
if (typeof process !== 'undefined') {
// Node.js
var fs = require('fs');
return fs.readFileSync(path, 'utf8');
} else {
// Rhino
return readFile(path);
}
}
function fileExists(path) {
if (typeof process !== 'undefined') {
// Node.js
var fs = require('fs');
try {
fs.statSync(path);
return true;
} catch (e) {
return false;
}
} else {
// Rhino
return (new java.io.File(path)).exists();
}
}
return {
loadConfig: function(configPath) {
if (fileExists(configPath)) {
return JSON.parse(readTextFile(configPath));
}
return {};
}
};
});Handle command-line execution errors and environment issues.
# Error exit codes
# 0 - Success
# 1 - General error
# 2 - Invalid arguments
# 3 - Module loading error
# 4 - Build errorUsage Examples:
# Check for errors in scripts
node r.js build.js
if [ $? -ne 0 ]; then
echo "Build failed!"
exit 1
fi
# Handle specific errors
node r.js main.js 2>&1 | tee log.txt
if grep -q "Error:" log.txt; then
echo "Application error detected"
# Send notification, cleanup, etc.
fi// Error handling in AMD modules
define(function() {
return {
main: function() {
try {
// Application logic
} catch (error) {
console.error('Application error:', error);
// Environment-specific error reporting
if (typeof process !== 'undefined') {
process.exit(1);
} else {
// Rhino - throw to exit with error
throw error;
}
}
}
};
});// Build configuration format
interface CommandLineBuildConfig {
// Inline configuration (space-separated key=value)
baseUrl?: string;
name?: string;
out?: string;
include?: string;
exclude?: string;
appDir?: string;
dir?: string;
optimize?: "uglify" | "closure" | "none";
// Or reference to JS configuration file
buildFile?: string;
}Usage Examples:
# Inline configuration
node r.js -o baseUrl=src name=main out=dist/app.js optimize=uglify
# Multiple modules
node r.js -o baseUrl=src dir=dist modules='[{name:"main"},{name:"admin"}]'
# Reference config file
node r.js -o myBuild.js
# Combination
node r.js -o myBuild.js optimize=none# Verify Node.js installation
node --version # Requires Node 0.4+
# Download r.js
curl -O https://github.com/jrburke/r.js/raw/master/dist/r.js
# Make executable (optional)
chmod +x r.js
# Run
node r.js main.js# Download required JARs
curl -O https://github.com/jrburke/r.js/raw/master/lib/rhino/js.jar
curl -O https://github.com/jrburke/r.js/raw/master/lib/closure/compiler.jar
# Set CLASSPATH (Linux/Mac)
export CLASSPATH="lib/rhino/js.jar:lib/closure/compiler.jar"
# Set CLASSPATH (Windows)
set CLASSPATH=lib\rhino\js.jar;lib\closure\compiler.jar
# Run
java org.mozilla.javascript.tools.shell.Main r.js main.js