NodeJS using a class from another file

nodejs export class
module.exports function with parameters
node js class
javascript import class from another file
module.exports multiple functions
javascript import class from another file es6
module.exports is not a function
javascript class

I have a java script file that is referencing another javascript file that contains a class using

const Champion = require("./championgg_webscraper_cheerio.js");

I then try to instantiate an object of the class Champion by

var temp = new Champion("hello");
console.log(temp);

And when I do it prints this to the console indicating and undefined variable:

Champion {}

Also when i try to print out the properties of the class I get undefined, I think it might not have access to the most_frequent_completed_build variable.

console.log(temp.most_frequent_completed_build);

Here is a look at the championgg_webscraper_cheerio.js file

function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
};
module.exports = Champion;

I think you want a Function constructor named Champion (a prototype or blue-print like classes in other programming languages like Java).

As an alternative I would suggest you to learn ES6 way of writing classes which is similar to that of Java.

You can achieve that by adding all the variables or methods to the this variable inside the Function Constructor so that you can access them using an object created using the 'new' keyword i.e make them Class members or methods.

In your case,

function Champion(champName) {
    //Some code

    this.most_frequent_completed_build = NULL;

    //Rest of code
}

module.exports = Champion;

Just make sure whenever you try to access Class variables always use this.variable_name like this.most_frequent_completed_build.

So when you create a new object of this Class in main app you will be able to access all Class members and methods.

const Champion = require("./championgg_webscraper_cheerio.js");

var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);

How to use module.exports in Node.js, Modules are therefore tied to files, with one module per file. If you are not familiar with using classes or other Node.js fundamentals, you can take a look at our  Questions: I have a java script file that is referencing another javascript file that contains a class using const Champion = require("./championgg_webscraper_cheerio.js"); I then try to instantiate an object of the class Champion by var temp = new Champion("hello"); console.log(temp); And when I do it prints this to the console indicating and undefined variable: Champion


You are exporting a function

All you have to do is call that function like

var temp = Champion();

You can read more about new keyword here and here

Getting started with Node.js modules: require, exports, imports and , In Node.js, all variables, functions, classes are visible to each other only within the same file. We are able to call functions from any other file. In Node.js, all variables, functions, classes are visible to each other only within the same file. We are able to call functions from any other file. To do this, we will use require and module.exports. First, create 2 files with which we work, server.js and index.js.


    function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
  return {most_frequent_completed_build:most_frequent_completed_build};
};
module.exports = Champion;

    var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);

How to use functions from another file in Node.js using module.exports, When you visit our site, pre-selected companies may access and use certain information on your device to serve relevant ads or personalized  How to instantiate a javascript class in another js file? In node.js, the recommended way instances passed between the files are not recognizing the class


In Node.js, how to import functions from another JavaScript file , James shows how to work with modules in Node.js, focusing on how to It's useful when a module needs to be imported by a number of different module loaders. Then in the index.js file, we're importing this function and executing it. an exports property on module — and of course, this works just fine: Node.js. Node.js is currently using a module.exports/require system. You can use babel to transpile if you want the import syntax. // mymodule.js exports.hello = function() { return "Hello"; } // server.js const myModule = require('./mymodule'); let val = myModule.hello(); // val is "Hello"


Understanding module.exports and exports in Node.js, This is often a relative or absolute path name to the .js file containing the module. Here, accessing the exports means using the module name It is possible to have a default export (whether it is an object, a function, a class, etc.). for Android, Opera for Android, Safari on iOS, Samsung Internet, Node.js  This article offers tips and code samples on creating and instantiating a class in Node.js. Check it out and learn more. How to Create & Instantiate a Class in Node.js - DZone Web Dev Web Dev Zone


import, Node.js will treat as CommonJS all other forms of input, such as .js files where Files ending with .mjs are always loaded as ES modules regardless of package scope. Condition matching is applied in object order from first to last within the​  can you include call the variable witouth requiring it?, what happens if you have a lot of files that must include the same variables or modules? do I have to use require for all? is there a global declaratiion in a main file so we can use them in all other files? – Ivan Juarez May 27 '17 at 17:02