Node.js · Junior · Question 12 of 75

How do you export a function in CommonJS?

function add(a, b) {
  return a + b;
}

Explanation

module.exports is the value that require() returns. export default is ES module syntax. You can also write exports.add = add, but not exports = { add }.

Further reading

Problem with a question

Node.js · Junior · question 12

Test yourself

Take the Node.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz