module - JavaScript exportimport doesn't work - Stack Overflow

it might be a silly question but I can't fix it anyway. I have a JavaScript file with various func

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

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

Share Improve this question asked Feb 5, 2018 at 8:23 zimmyblnzimmybln 1593 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744360533a4570449.html

相关推荐

  • module - JavaScript exportimport doesn&#39;t work - Stack Overflow

    it might be a silly question but I can't fix it anyway. I have a JavaScript file with various func

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信