Home » Javascript » How to export an object from a separate file to both Node and Javascript?

How to export an object from a separate file to both Node and Javascript?

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have a file called utils.js which has some constants and I’m building a socket app which uses Node for backend work and regular Javascript for frontend. The names of the socket events are identical in both frontend and backend so I want to use the constants both in backend and frontend. But the syntax for exporting/importing is different in ecmascript6 as opposed to Node so how do I pull this off?

This is the contents of utils.js:

const EVENTS = {
    CONNECTION: 'connection',
    MESSAGE: 'message'
};
Object.freeze(EVENTS);
module.exports = EVENTS;
Answers: