To make objects available outside of a module you just need to expose them as additional properties on the exports object.
1 Create square.js that exports methods.
exports.area = function(width) { return width * width; };
exports.perimeter = function(width) { return 4 * width; };
2 Import this on express app.js
const square = require('./square'); // Here we require() the name of the file without the (optional) .js file extension
console.log('The area of a square with a width of 4 is ' + square.area(4));
3 Result
If you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports
module.exports = {
area: function(width) {
return width * width;
},
perimeter: function(width) {
return 4 * width;
}
};
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction