CommonJS to AMD Converter
Convert CommonJS modules to AMD format online. Wraps require() and module.exports in define() blocks instantly. Free browser-based tool.
About
JavaScript module systems are not interchangeable. A file using require() and module.exports follows the CommonJS specification designed for Node.js synchronous loading. AMD (define()) targets asynchronous browser environments via RequireJS. Deploying CommonJS code directly in an AMD pipeline causes silent runtime failures: undefined dependencies, broken dependency graphs, and load-order race conditions. This tool performs the same wrapping operation as the RequireJS Optimizer's internal commonJs.convert function. It detects whether a file already contains a define() call and skips conversion if so. It also verifies the presence of CommonJS signatures (require(), exports, or module.exports) before wrapping. Files that match neither pattern pass through unchanged.
Limitations apply. This converter handles standard single-file CommonJS modules. It does not resolve dependency trees, rewrite paths, or handle dynamic require() calls computed at runtime. Circular dependencies remain the developer's responsibility. For multi-file batch conversion, replicate this logic in a Gulp or Grunt pipeline. The wrapping template matches the output of gulp-require-convert and is compatible with RequireJS 2.x and r.js optimizer.
Formulas
The conversion follows a deterministic rule-based algorithm rather than a mathematical formula. The logic can be expressed as a decision function:
Where hasDefine(input) tests the regex pattern /define\s*\(/ against the source. The function hasCJS(input) tests for the presence of any of these patterns: require("..."), module.exports, or bare exports. references. The wrap function constructs the output as:
Where indent(input) prepends the configured indentation string (default: 4 spaces) to each non-empty line of input. If a shebang line (#!/usr/bin/env node) is detected at position 0, it is extracted and placed above the define() wrapper to maintain compatibility with command-line execution.
Reference Data
| Feature | CommonJS (CJS) | AMD | ES Modules (ESM) | UMD |
|---|---|---|---|---|
| Specification | Node.js / ServerJS | RequireJS | ECMAScript 2015+ | Universal wrapper |
| Loading | Synchronous | Asynchronous | Static & Async | Both |
| Import syntax | require() | define() / require() | import | require() or define() |
| Export syntax | module.exports | return value | export | module.exports |
| Browser native | No | No (needs loader) | Yes (modern) | No |
| Node.js native | Yes | No | Yes (.mjs / type:module) | Yes (CJS path) |
| Tree shaking | Limited | No | Yes | No |
| Circular deps | Partial (cached) | Partial | Live bindings | Partial |
| Dynamic import | require(expr) | require([arr], cb) | import() | Via CJS or AMD path |
| Bundler support | Webpack, Browserify | RequireJS, Dojo | Webpack, Rollup, Vite | All bundlers |
| File extension | .js / .cjs | .js | .mjs / .js | .js |
| Conditional exports | Runtime branching | Plugin-based | Package.json exports map | Runtime branching |
| Use case | Server-side, legacy | Legacy browser apps | Modern apps | Libraries targeting both |
| Year introduced | 2009 | 2011 | 2015 | 2012 |
| Wrapper overhead | None | ~80 bytes | None | ~200 bytes |