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
- Visit the RequireJS download page
- Download the latest release
- Include the
require.jsfile 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 filerequire.config.js: Optional configuration file (can be embedded in HTML)
Build & Run
Basic Usage
- Include RequireJS in your HTML:
<script data-main="scripts/main" src="scripts/require.js"></script>
- Create a main.js configuration file:
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app'
}
});
requirejs(['app/main'], function(main) {
// Application logic here
});
Running Tests
- Ensure all dependent projects are cloned as siblings to the requirejs directory
- Start a local web server in the parent directory
- Open
requirejs/tests/index.htmlin your browser - Click the arrow button to run all tests
Development vs Production
- Development: Use unminified
require.jsfor 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
-
Module Not Found Errors
- Ensure correct
baseUrlandpathsconfiguration - Check file paths and naming conventions
- Verify modules are defined using
define()
- Ensure correct
-
Circular Dependencies
- Review module dependencies
- Use
shimconfiguration for non-AMD libraries - Consider refactoring module structure
-
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.jsduring development - Check browser developer tools for loading and execution errors
- Use
requirejs.onErrorto 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