Search code examples
javascriptecmascript-6es6-module-loader

Why can't I export a function named "import" in ES6


I want to export a function I named "import" like this:

export function import(foo, bar) {
    console.log(foo + bar);
}

However for some reason the es6 linter complains that "import is not a valid identifier for a function" see this fiddle

What's wrong? Can't I have my function called import in es6? what about export?


Solution

  • import and export are reserved words. You can't use them as the name of a function declaration.

    You can however still use them as a name for your exports - you just can't declare a variable with it:

    function _import(foo, bar) {
        console.log(foo + bar);
    }
    export {_import as import};
    

    I would recommend against it though, it complicates importing similarly.