1

When I run this code written in Javascript:

const addition = (a, b) => {
    return a + b;
}
console.log(addition(5, 2));
export {addition};

I get this error:

export {addition};
^^^^^^
SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

I use VS Code. I am quite new to programming.

rioV8
  • 18,123
  • 3
  • 18
  • 35
LCAYSS
  • 11
  • 1

1 Answers1

-1

You need to export the variable at the declaration time like this,

export const addition = (a, b) => {
    return a + b;
}
console.log(addition(5, 2));
Shridhar Sharma
  • 2,265
  • 1
  • 8
  • 13
  • There is nothing wrong with the way the export is handled in the question (that isn't also wrong with your code). The problem is that the ES6 module is being executed with a CommonJS loader, and the two are not compatible. – Quentin Oct 06 '20 at 17:54