Forum Discussion

jfk's avatar
jfk
New Contributor
7 years ago
Solved

how to import a javascript class

hi, I would like to import a javascript class to another file but unfortunately I couldn't successfully do that :) I tried something like this:     // fileA class A{ constructor(x){   th...
  • tristaanogre's avatar
    7 years ago

     

    Your syntax is a bit off.  When you do modules.exports.class = A; you're basically saying that the class "A" is exported to other modules as "class".  Try the following and see if it works (worked for me).

     

    // fileA
    class A{
        constructor(x){
          this.x = x
    }
        getX(){
          return this.x
     }
    }
    module.exports.A = A;
    
    //fileB
    var ss = require("fileA");
    
    var tt =  new ss.A("2")