← Back to requirejs/requirejs

How to Deploy & Use requirejs/requirejs

RequireJS Deployment and Usage Guide

Prerequisites

  • Runtime: Modern web browser (IE 6+, Firefox 2+, Safari 3.2+, Chrome 3+, Opera 10+)
  • Development Environment: Any text editor or IDE for JavaScript development
  • Testing Environment: Local web server (for running tests)
  • Git: Required for cloning the repository and dependent projects
  • Internet Connection: Required for running tests that access remote URLs

Installation

Option 1: Download from Official Site

  1. Visit the RequireJS download page
  2. Download the latest release
  3. Include the require.js file in your project

Option 2: Clone from GitHub

# Clone the repository
git clone https://github.com/requirejs/requirejs.git

# Navigate to the project directory
cd requirejs

# Clone dependent projects (required for running tests)
git clone https://github.com/requirejs/text.git
git clone https://github.com/requirejs/i18n.git
git clone https://github.com/requirejs/domReady.git

Option 3: Using npm (for Node.js environments)

npm install requirejs

Configuration

Basic Configuration

Create a requirejs.config object to configure RequireJS:

requirejs.config({
    baseUrl: 'js/lib',
    paths: {
        app: '../app'
    },
    shim: {
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        }
    }
});

Environment Variables

No specific environment variables are required for basic usage.

Configuration Files

  • require.js: The main loader file
  • require.config.js: Optional configuration file (can be embedded in HTML)

Build & Run

Basic Usage

  1. Include RequireJS in your HTML:
<script data-main="scripts/main" src="scripts/require.js"></script>
  1. Create a main.js configuration file:
requirejs.config({
    baseUrl: 'js/lib',
    paths: {
        app: '../app'
    }
});

requirejs(['app/main'], function(main) {
    // Application logic here
});

Running Tests

  1. Ensure all dependent projects are cloned as siblings to the requirejs directory
  2. Start a local web server in the parent directory
  3. Open requirejs/tests/index.html in your browser
  4. Click the arrow button to run all tests

Development vs Production

  • Development: Use unminified require.js for easier debugging
  • Production: Use the optimization tool to combine and minify files

Deployment

Web Server Deployment

RequireJS is designed for browser use and can be deployed on any web server:

# Deploy to Apache/Nginx
# Simply copy your HTML, JavaScript, and require.js files to your web root

Optimization for Production

Use the RequireJS optimization tool to improve performance:

# Install the optimizer globally
npm install -g requirejs

# Optimize your project
r.js -o build.js

Platform Recommendations

  • Static Hosting: Netlify, Vercel, GitHub Pages
  • Traditional Hosting: Apache, Nginx, IIS
  • Cloud Platforms: AWS S3 + CloudFront, Google Cloud Storage

Troubleshooting

Common Issues

  1. Module Not Found Errors

    • Ensure correct baseUrl and paths configuration
    • Check file paths and naming conventions
    • Verify modules are defined using define()
  2. Circular Dependencies

    • Review module dependencies
    • Use shim configuration for non-AMD libraries
    • Consider refactoring module structure
  3. Loading Issues in Production

    • Use the optimization tool to combine files
    • Ensure correct file permissions on the server
    • Check browser console for specific error messages

Debugging Tips

  • Use the unminified version of require.js during development
  • Check browser developer tools for loading and execution errors
  • Use requirejs.onError to catch global errors:
requirejs.onError = function(err) {
    console.log(err.requireType);
    if (err.requireType === 'timeout') {
        console.log('modules: ' + err.requireModules);
    }
};

Performance Issues

  • Use the optimization tool to reduce HTTP requests
  • Implement proper caching strategies
  • Consider using a CDN for RequireJS if appropriate for your project

Testing Issues

  • Ensure all dependent projects are cloned as siblings
  • Verify internet connectivity for remote URL tests
  • Use a local web server instead of file:// protocol for testing