close

[Solved]Getting Unexpected Token Export

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Getting Unexpected Token Export in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Getting Unexpected Token Export Error Occurs?

Today I get the following error Getting Unexpected Token Export in javascript.

How To Solve Getting Unexpected Token Export Error ?

  1. How To Solve Getting Unexpected Token Export Error ?

    To Solve Getting Unexpected Token Export Error To use ES6 add babel-preset-env
    and in your .babelrc:
    { "presets": ["@babel/preset-env"] }
    Answer updated thanks to @ghanbari comment to apply babel 7.

  2. Getting Unexpected Token Export

    To Solve Getting Unexpected Token Export Error To use ES6 add babel-preset-env
    and in your .babelrc:
    { "presets": ["@babel/preset-env"] }
    Answer updated thanks to @ghanbari comment to apply babel 7.

Solution 1

You are using ES6 Module syntax.

This means your environment (e.g. node.js) must support ES6 Module syntax.

NodeJS uses CommonJS Module syntax (module.exports) not ES6 module syntax (export keyword).

Solution:

  • Use babel npm package to transpile your ES6 to a commonjs target

or

  • Refactor with CommonJS syntax.

Examples of CommonJS syntax are (from flaviocopes.com/commonjs/):

  • exports.uppercase = str => str.toUpperCase()
  • exports.a = 1

Solution 2

To use ES6 add babel-preset-env

and in your .babelrc:

{
  "presets": ["@babel/preset-env"]
}

Answer updated thanks to @ghanbari comment to apply babel 7.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment