Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
why module exports not working in javascript?

I want to use module.exports to move User to another folder, but it is not working

The console give this error

script.js:542 Uncaught ReferenceError: module is not defined

class User {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }
  courseList = [];
  getInfo() {
    return {
      name: this.name,
      email: this.email
    };
  }

  enrollCourse(name) {
    this.courseList.push(name);
  }
  getCourseList() {
    return this.courseList;
  }
}

module.exports= User; 

This in the new folder

import User from "./script";

const piyush = new User("piyush", "piyush@gmail.com");

console.log(piyush);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is wrong way to export module.exports.User change this to this module.exports = Users; and assign your class to this Users variable. after that require it in the file where you want to use it. final code

var Users = class User {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }
  courseList = [];
  getInfo() {
    return {
      name: this.name,
      email: this.email
    };
  }

  enrollCourse(name) {
    this.courseList.push(name); // in console this will as show as undefine because you not using return
    // return this.courseList.push(name); // if you use return then this will show 1 means items get added in your array.
  }
  getCourseList() {
    return this.courseList;
  }
}

module.exports = Users;

this is for new folder

var Users = require("./javascript.js");

const piyush = new Users("piyush", "piyush@gmail.com");
console.log(piyush); // get constructor from your json.
console.log(piyush.getInfo()); // get getInfo from your json.
console.log(piyush.enrollCourse("cse")); // get enrollCourse from your json.
console.log(piyush.getCourseList()); // get getCourseList from your json.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda