Sharing constants between a web client and NodeJS server

I have a NodeJS application with common constants between the client and the server.

The constants are stored in variables rather than inline. Those variables could be defined in two separate files, one for client and one for server.

File 1:

// client_constants.js
MESSAGE_TYPE_A = "a";
MESSAGE_TYPE_B = "b";

File 2:

// server_constants.js
exports.MESSAGE_TYPE_A = "a";
exports.MESSAGE_TYPE_B = "b";

To avoid duplicate code I would like to store constants in a single location and a single format for both the client and the server. Wat do?

You can do something like this:

// constants.js
root = exports ? window
root.MESSAGE_TYPE_A = "a";
root.MESSAGE_TYPE_B = "b";

"exports" does not exist on the client side in which case it will use the default "window" object.