0

I usenode.js and I loaded js file in ejs

When main.js will be loaded,script will be functioned. while then,

    <script type="text/javascript" src=../javascripts/functions.js></script>
    <script type="text/javascript" src=../javascripts/quiz.js></script>
    <script type="text/javascript" src=../javascripts/main.js></script>

I have some issues like Uncaught ReferenceError: module is not defined in quiz.js

How to set export correctly ?

I'd like to export quiz class correctly. Is this issue comes from ES6 ?

If someone has opinion,please let me know.

Thanks

quiz.js

class Quiz {
  
    constructor(quizData){
        this._quizzes = quizData.results;
        this._correctAnswersNum = 0;
    }
 
    getQuestion(index){
        return this._quizzes[index-1].question;
    }
    
}

module.exports = Quiz;

main.js


(()=>{
      const url = "/quiz-data";

      fetch(url)
      .then(response => response.json())
      .then(json => {
        const quiz = new Quiz(json);
        console.log("json",json);
        console.log("quiz",quiz);
        displayQuiz(quiz,1);
      });
})();

1 Answer 1

1

Client-side JS does not support Node.js style CommonJS modules.


If you want to use modules client-side, then write ES6 modules.


If you just want to use shared globals, then write the code as you have it now only without the module.exports line.

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

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.