Include es6 class from external file in Node.js
export class MyClass
{
constructor(arg){
this.arg = arg;
}
}
import {MyClass} From'./MyClass.js'
class OtherClass{
arg_two;
}
import {OtherClass} From'./OtherClass.js'
class1 = new OtherClass();
class1.arg_two.arg = 'class';
console.log(class1.arg_two.arg);
Either do
module.exports = class MyClass {
constructor(arg){
console.log(arg);
}
};
and import with
var a = require("./class.js");
new a("fooBar");
or use the newish syntax (may require you to babelify your code first)
export class MyClass {
constructor(arg){
console.log(arg);
}
};
and import with
import {myClass} from "./class.js";
export default class myClass {
constructor(arg){
console.log(arg);
}
}
Other file:
import myClass from './myFile';