0

I'd like to import test.js in main.js,I suffered some following errors..

I totally confused how to fix it and how to export correctly..

SyntaxError: Unexpected token export

If someone has opinion,please let me know.

Thanks

test.js


export default class Test {

    constructor(quizData){
        this._quizzes = quizData.results;
        this._correctAnswersNum = 0;
    }
    
    getNumOfQuiz(){
        return this._quizzes.length;
    }
}

main.js


import Test from './test.js';
// require('./quiz.js')

(()=>{
      
      const url = "/quiz-data";
      
      console.log("main.js was loaded");
      
      fetch(url)
      .then(json => { const quiz = new Quiz(json); })
      .then(quiz => displayQuiz(quiz,1))

})();
Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • It seems that your env't doesn't support ES6 imports – hindmost Aug 13 '20 at 13:30
  • Where are you running this code, Node environment? Also `export` (es6) and `module.exports` (commonjs) are different module systems. – ambianBeing Aug 13 '20 at 13:33
  • NodeJS uses CommonJS for imports/exports, that's what the `modules.exports` is. JavaScript's module syntax uses the `export` keyword and should be supported in node versions >= 12. – Nick Parsons Aug 13 '20 at 13:35

1 Answers1

0

Remove module.exports = Quiz; from your code. It will start working Please refer to https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export for es6 import and export.

devd
  • 693
  • 4
  • 11
  • After remove, it still warn same errors.. I guess there is another point of this error. – Heisenberg Aug 13 '20 at 13:30
  • Does your environment support es6 import and export? If not then please follow the link to know how to do it correctly.https://medium.com/@zachgavin/module-exports-and-loading-es5-to-es6-a33ac592989c – devd Aug 13 '20 at 13:34