Exposing modules in library




I’ve been developing on OCaml projects for some time now, however, it is my first time writing an OCaml library using dune.

My main issue is that I want my library to expose several modules (in particular, some modules contains functions / functors that can be used by the users, and other modules contains types (and related functions) that should be used to construct parameters for said functions / functors.

What is the best practice for doing this ?
The solution I use is simply to import these modules in the main library file, which will contain

module FirstExposedMod = FirstExposedMod
module SecondExposedMod = SecondExposedMod
module ThirdExposedMod = ThirdExposedMod
module FourthExposedMod = FourthExposedMod

Is it the best way of doing things ? I haven’t been able to find informations on that anywhere.

Thank you very much !


Hi @giltho,

If you want to expose all of the modules of your library, then you can simply skip writing a main library file, and dune will simply expose all the modules.

If you want to hide a few of them because they are private, then you can use the private_modules field:

 (name foo)
 (private_modules common utils))

Writing an explicit main library file is only relevant in the following cases:

  • when you want to expose some modules with a different name/path, for instance module X = Foo.Bar.X
  • when you want to have toplevel values in your library
  • when you simply want to expose a different API to the outside world

I hope that answers your question.


Hi @diml,

This answers my question perfectly thank you very much.
Dune keeps surprising me with its elegance as I use it.

Thank you !


Since when do we have private_modules?
I needed that one.


It was added in 1.2.0; you can find the full changelog here.