1.

What is the purpose of module.exports?

Answer»

This is used to expose functions of a PARTICULAR MODULE or file to be used elsewhere in the project. This can be used to encapsulate all SIMILAR functions in a file which further improves the project structure.

For EXAMPLE, you have a file for all utils functions with util to get solutions in a different programming language of a problem statement.

const getSolutionInJavaScript = ASYNC ({ problem_id}) => {...};const getSolutionInPython = async ({ problem_id}) => {...};module.exports = { getSolutionInJavaScript, getSolutionInPython }

Thus using module.exports we can use these functions in some other file:

const { getSolutionInJavaScript, getSolutionInPython} = require("./utils")


Discussion

No Comment Found