0

I would like to know the difference between creating a class and exporting it later and creating a class with the keyword export at the beginning

First use case:

class Foo {
  constructor() {
  }
  // ...
}
export = Foo;

Second use case:

export class Foo {
  constructor() {
  }
  // ...
}

The reason I am asking is that when I use the first approach, I always have to import the class in another module like this:

import Foo = require("./Foo");

and when I use the second approach, I can import it like this:

import {Foo} from "./Foo";

I have two questions: Whats the difference between these two approaches? and is there a way to export the class to import a class without using require while keeping the first approach ?

Pryda
  • 899
  • 5
  • 14
  • 37
  • Does this answer your question? [The difference between "require(x)" and "import x"](https://stackoverflow.com/questions/46677752/the-difference-between-requirex-and-import-x) – Terry Jun 26 '20 at 07:45
  • 1
    The real difference is between `export = Foo` and `export Foo`. Try getting rid of the `=`… – deceze Jun 26 '20 at 07:47

1 Answers1

1

Require Is a method Introduced By NodeJs, While Import is a EcmaScript/TypeScript method,

You can use both in nodejs because nodejs supports EcmaScript too, but you cannot use require in other programs