3

it might be a silly question but I can't fix it anyway. I have a JavaScript file with various functions I'd like to export.

export function AddNumbers(...numbers)
{
    let value = 0;

    for(var i = 0;i < numbers.length;i++)
    {
        value += numbers[i];
    }

    return value;
}  

When I call this method (using mocha) I get an error message "export function AddNumbers(...numbers) Unexpected token export". The project is build as ES6. Does anybody know what I'm doing wrong?

Best regards, Torsten

2 Answers 2

3

You need to use module.exports as NodeJS uses CommonJS Module syntax which requires to use module.exports and not just export which is defined by ES6 module syntax. So, make sure CommonJS is also configured properly in your project.

Sign up to request clarification or add additional context in comments.

Comments

1

Another solution is to use Babel. Install it with

npm install babel-core --save-dev
npm install babel-preset-es2015 --save-dev

Create in root directory a file .babelrc with following content

{
    "preset" : ["es2015"]
}

and finally change the script in package.json to run into:

"scripts": {
    "test": "mocha Tests --require babel-core/register"
}

and now export / import works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.