The moduli space of canonical divisors (with prescribed zeros and poles) on nonsingular curves is not compact since the curve may degenerate. We define a proper moduli space of twisted canonical divisors in the moduli space of Deligne-Mumford stable pointed curves which includes the space of canonical divisors as an open subset. The theory leads to geometric/combinatorial constraints on the closures of the moduli spaces of canonical divisors. In case the differentials have at least one pole (the strictly meromorphic case), the moduli spaces of twisted canonical divisors on genus g curves are of pure codimension g in the moduli spaces of stable pointed curves. In addition to the closure of the canonical divisors on nonsingular curves, the moduli spaces have virtual components. In the Appendix, a complete proposal relating the sum of the fundamental classes of all components (with intrinsic multiplicities) to a formula of Pixton is proposed. The result is a precise and explicit conjecture in the tautological ring for the weighted fundamental class of the moduli spaces of twisted canonical divisors. As a consequence of the conjecture, the classes of the closures of the moduli spaces of canonical divisors on nonsingular curves are determined (both in the holomorphic and meromorphic cases).