A command line tool for running JavaScript scripts that use the Asynchronous Module Definition API (AMD) for declaring and using JavaScript modules and regular JavaScript script files.
—
RequireJS r.js provides a comprehensive command line interface for building, optimizing, and running AMD-based JavaScript applications. The CLI supports multiple JavaScript environments including Node.js, Rhino, Nashorn, and xpcshell.
# Install globally via npm
npm install -g requirejs
# Or use locally
npm install requirejs
# Run r.js directly (Node.js)
node r.js [options] [script.js]
# Use global installation
r.js [options] [script.js]r.js [command-option] [arguments]Command Options:
-o - Run optimizer/build-v - Display version information-convert - Convert CommonJS modules to AMD-lib - Load bundled librariesExecute AMD-based JavaScript applications:
# Run main application script
r.js main.js
# Run with specific module as entry point
r.js app/startup.js
# Run script in current directory (defaults to main.js if no file specified)
r.jsDisplay RequireJS version and environment details:
# Show version information
r.js -v
# Sample output:
# r.js: 2.3.7, RequireJS: 2.3.7, Env: node, Command line args: -v# Build using configuration file
r.js -o build.js
# Build using configuration object
r.js -o baseUrl=src name=main out=dist/main-built.jsPass build options directly via command line:
# Single module optimization
r.js -o name=main baseUrl=js out=built/main.js
# Multi-option build
r.js -o appDir=src dir=dist baseUrl=. optimize=uglify
# Specify configuration file
r.js -o build/config.build.js
# Multiple configuration sources
r.js -o build.js baseUrl=override optimize=none# CSS optimization
r.js -o cssIn=css/main.css out=css/main-built.css optimizeCss=standard
# Source map generation
r.js -o build.js generateSourceMaps=true optimize=uglify2
# Skip directory optimization
r.js -o build.js skipDirOptimize=true
# Preserve license comments
r.js -o build.js preserveLicenseComments=trueConvert CommonJS modules to AMD format:
# Convert single directory
r.js -convert path/to/commonjs/modules path/to/amd/modules
# Convert with specific options
r.js -convert src/commonjs dist/amdConversion Process:
define() callsrequire() calls to dependency arraysmodule.exports and exports assignments# Input CommonJS module (math.js):
var helper = require('./helper');
module.exports = {
add: function(a, b) { return a + b; }
};
# After conversion:
define(['./helper'], function(helper) {
return {
add: function(a, b) { return a + b; }
};
});Load and use RequireJS with bundled libraries:
# Load with library support
r.js -lib script.js
# Available in script.js:
# - requirejs/require/define functions
# - Bundled utility libraries
# - Environment abstraction# Standard Node.js execution
node r.js -o build.js
# Use specific Node.js options
node --max-old-space-size=4096 r.js -o large-build.js
# Environment variable configuration
NODE_ENV=production node r.js -o build.js# Rhino execution with classpath
java -classpath rhino.jar:. org.mozilla.javascript.tools.shell.Main r.js -o build.js
# Nashorn execution (Java 8+)
jjs r.js -- -o build.js
# With Closure Compiler
java -Xmx1g -classpath rhino.jar:closure-compiler.jar org.mozilla.javascript.tools.shell.Main r.js -o build.js optimize=closure# Firefox extension development
xpcshell r.js -o addon-build.js
# With Mozilla-specific options
xpcshell -f r.js -e "load('build-script.js')"// build.js
({
appDir: "src/",
baseUrl: "./",
dir: "dist/",
modules: [
{ name: "main" }
],
optimize: "uglify"
})r.js -o build.js{
"appDir": "src/",
"baseUrl": "./",
"dir": "dist/",
"modules": [
{ "name": "main" }
],
"optimize": "uglify"
}r.js -o build.json# Inline configuration (single line)
r.js -o "({appDir:'src',dir:'dist',modules:[{name:'main'}]})"
# Mixed file and inline options
r.js -o build.js optimize=none generateSourceMaps=true# Write optimized output to stdout
r.js -o build.js out=stdout
# Combine with other tools
r.js -o name=main baseUrl=src out=stdout | gzip > main.js.gz
# Silent operation
r.js -o build.js logLevel=4# Trace level (most verbose)
r.js -o build.js logLevel=0
# Info level (default)
r.js -o build.js logLevel=1
# Warning level
r.js -o build.js logLevel=2
# Error level
r.js -o build.js logLevel=3
# Silent (no output)
r.js -o build.js logLevel=4# Missing build file
r.js -o nonexistent.js
# Error: ENOENT: no such file or directory
# Invalid configuration
r.js -o "({invalid:syntax})"
# Error: SyntaxError: Unexpected token
# Module not found
r.js -o name=missing baseUrl=src out=dist/missing.js
# Error: Module name "missing" has not been loaded yet# Verbose output with trace information
r.js -o build.js logLevel=0
# Preserve intermediate files for debugging
r.js -o build.js keepBuildDir=true
# Generate source maps for debugging minified code
r.js -o build.js generateSourceMaps=true
# Skip minification for debugging
r.js -o build.js optimize=none# Fast development builds
r.js -o build.js optimize=none skipDirOptimize=true
# Maximum optimization
r.js -o build.js optimize=uglify generateSourceMaps=false removeCombined=true
# Memory usage control (Node.js)
node --max-old-space-size=8192 r.js -o large-build.js# Keep previous build directory for faster rebuilds
r.js -o build.js keepBuildDir=true
# Only rebuild changed modules
r.js -o build.js skipDirOptimize=true#!/bin/bash
# build.sh
# Development build
echo "Building development version..."
r.js -o build-dev.js
# Production build
echo "Building production version..."
r.js -o build-prod.js
# Create source maps
echo "Generating source maps..."
r.js -o build-prod.js generateSourceMaps=true{
"scripts": {
"build": "r.js -o build.js",
"build:dev": "r.js -o build-dev.js",
"build:prod": "r.js -o build-prod.js",
"watch": "nodemon --exec 'npm run build:dev' --watch src/",
"convert": "r.js -convert src/commonjs dist/amd"
}
}# CI build script
set -e # Exit on any error
echo "Converting CommonJS modules..."
r.js -convert lib/commonjs lib/amd
echo "Running build optimization..."
r.js -o build-ci.js
echo "Build completed successfully"# Show help information
r.js -help
r.js --help
r.js -h
# Online documentation reference
# See https://github.com/requirejs/r.js for usage.The r.js command line tool provides a flexible interface for all RequireJS optimization and execution needs, supporting multiple JavaScript environments and extensive configuration options for modern web application builds.
Install with Tessl CLI
npx tessl i tessl/npm-requirejs