Quiero usar module.exports para mover Usuario a otra carpeta, pero no funciona
La consola da este error.
script.js:542 Error de referencia no capturado: el módulo no está definido
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;Esto en la carpeta nueva
import User from "./script"; const piyush = new User("piyush", "piyush@gmail.com"); console.log(piyush);Es una forma incorrecta de exportar module.exports.User , cambie esto a este module.exports = Usuarios; y asigne su clase a esta variable Usuarios. después de eso, pídelo en el archivo donde quieras usarlo. codigo final
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;esto es para nueva carpeta
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.