Command-line options
This page documents the command-line options for the js_of_ocaml compiler. Most of these options also apply to wasm_of_ocaml. Differences are noted where applicable.
General options
Option | Description |
| Display the compiler version |
| Set output filename |
| Set optimization profile (default: 1). See Optimizations |
| Enable option. See Enable/Disable options |
| Disable option. See Enable/Disable options |
| Add directory to the list of include directories |
Debugging
Option | Description |
| Pretty print JavaScript output, preserve OCaml variable names |
| Disable code inlining |
| Output debug information (source locations) |
| Generate source map. See Source maps |
Target environment
Option | Description |
| Build for target environment (default: |
Possible values:
isomorphic- Runs in both browser and Node.js (default)browser- Browser-specific, smaller outputnodejs- Node.js-specific, smaller output
Effect handlers
Option | Description |
| Select effect handler implementation |
For js_of_ocaml (default: disabled):
disabled- No effect handler support (default)cps- CPS transformation for effect supportdouble-translation- Keep both direct and CPS versions; choose at runtime
For wasm_of_ocaml (default: jspi):
jspi- JavaScript-Promise Integration (default). Available in Chrome 137, Node.js 25, and higher. Usecpsfor other browsers. Performing effects is slower than withcps.cps- CPS transformation for effect supportnative- WebAssembly stack switching (wasm_of_ocaml only). Available, behind the--experimental-wasm-wasmfxflag, in Chrome 148 or higher, or in a recent Node.js canary release (V8 version 14.7.100 or higher).disabled- No effect handler support
See Effect handlers for details.
Source maps
Option | Description |
| Generate source map (separate file) |
| Don't generate source map |
| Inline source map in the JavaScript output |
| Don't embed source code in source map |
| Set root directory for source map paths |
Pseudo filesystem
Js_of_ocaml provides a pseudo filesystem for programs that read files at runtime. The --file flag is also supported by wasm_of_ocaml.
Option | Description |
| Register file to pseudo filesystem (default target: |
| Write filesystem data to separate file (js_of_ocaml only) |
| Allow registering files from outside at runtime (default, js_of_ocaml only) |
| Only allow files embedded at compile time (js_of_ocaml only) |
Environment variables
Option | Description |
| Set environment variable at compile time |
Resolution order
When OCaml code calls Sys.getenv, js_of_ocaml resolves variables in this order:
- If the variable was set at compile time with
--setenv VAR=value, return it - If running in Node.js, check
process.env - Check
globalThis.jsoo_env(useful for setting variables in browsers) - Raise
Not_found
Browser example
// Set environment variables before loading the OCaml program
globalThis.jsoo_env = {
DEBUG: "true",
API_URL: "https://api.example.com"
};Toplevel and dynamic linking
Option | Description |
| Compile an OCaml toplevel (embeds necessary .cmi files) |
| Don't include .cmi files in toplevel |
| Enable dynamic linking of bytecode |
| File listing units to export for toplevel/dynlink |
| Link all compilation units and primitives |
See Building a toplevel for details.
Compilation modes
Option | Description |
| Don't include the standard runtime |
| Include partial runtime when compiling .cmo/.cma |
| Preserve unit names (for separate compilation) |
| Wrap output in a function (default: IIFE) |
| Add custom header (e.g., |
See Compilation modes for details.
Optimizations
Optimization profiles
Use --opt <level> to select a profile:
--opt 1- Basic optimizations (default)--opt 2- Standard optimizations--opt 3- Maximum optimizations (iterates until fix-point)
Recommended settings
For debugging:
js_of_ocaml --pretty --no-inline program.byte
For production:
js_of_ocaml --opt 3 program.byte
Enable/Disable options
Use --enable <opt> or --disable <opt> to control these flags:
Output formatting
Option | Default | Description |
| false | Pretty print JavaScript output |
| true | Use short variable names |
| true | Generate "use strict" |
| true | Include compiler version header |
| false | Generate ES6 syntax |
Optimizations
Option | Default | Description |
| true | Dead code elimination |
| true | Global dead code elimination |
| true | Function inlining |
| true | Static evaluation of constants |
| true | Share string and number constants |
| true | JavaScript call optimizations |
| true | Move constant declarations closer to usage sites |
| true | Coalesce JavaScript variables with disjoint lifespans |
Runtime behavior
Option | Default | Description |
| false | Enable effect handlers |
| true | Wrap JavaScript exceptions in OCaml exceptions |
| false | Attach JavaScript stack traces to OCaml exceptions |
| true | Keep |
| true | Generate dummy primitives when missing |
| true | Use JavaScript strings internally |
Internal debugging
Option | Default | Description |
| false | Include source location comments |
| false | Use stable (predictable) variable names |
| true | link required compilation units only |
Compiler parameters
Use --set <param>=<value> to tune compiler behavior:
Parameter | Default | Description |
| 60 | Max cases before switch becomes if-else chain |
| 150 | Size threshold for function inlining |
| 50 | Max tail-call depth before trampoline |
| trampoline | Tail-call mode: |
Warnings
Option | Description |
| Suppress non-error messages |
| Treat warnings as errors |
| Enable warning |
| Disable warning |
Available warnings:
Warning | Default | Description |
| on | Missing JavaScript primitive implementation |
| on | Missing .cmi file |
| on | Missing dependencies |
| on | Using effects, but not backend was selected |
| on | Integer overflow in constant |
| on | Overriding an existing primitive |
| on | Using deprecated primitive |
| on | Free variables in primitive code |
Debug output
Use --debug <section> to enable debug output for compiler internals:
times, stats, deadcode, globaldeadcode, inlining, constant-sinking, gen, linker, sourcemap, flow, parser, var-coalescing
Subcommands
The js_of_ocaml command has several subcommands:
Command | Description |
| Compile bytecode to JavaScript (default) |
| Link JavaScript files together |
| Build standalone runtime |
| Build pseudo filesystem |
The wasm_of_ocaml command has similar subcommands:
Command | Description |
| Compile bytecode to WebAssembly (default) |
| Link files together |
| Build standalone runtime |
Link command
js_of_ocaml link -o output.js runtime.js lib1.js lib2.js main.js
Additional options:
Option | Description |
| Build a library (.cma.js) |
| Resolve source map URLs |
Build-runtime command
js_of_ocaml build-runtime -o runtime.js
Accepts the same --enable/--disable options as the main command.
Build-fs command
js_of_ocaml build-fs -o fs.js file1.txt file2.dat
Builds a JavaScript file containing filesystem data.