No, this is not how modules are used. You are trying to use CommonJS syntax with its require
and module.exports
. With classes, you rather need ES6 syntax, that is, defined by ECMAScript standards starting v.6. Please see this Stackoverflow answer. If by some reason you have to use CommonJS way (one case where it is still required is the Visual Studio Code extension API), you can restructure your code to do so, we can discuss it. Let's see how to do it with ES6.
In one module, you can define and export a class, for example,
export class Movie { /* ... */ }
Alternatively, you can export several objects in one line:
export { Movie, AnotherClass, SomeFunction, someObject };
To use the class Movie
outside this module, you need to import it
import { Movie } from "relative path to your module file name";
const movieInstance = new Movie(/* ... */);
// ...
movieInstance.displayMovieDetails();
Note that you can selectively choose what to import. You can also use alias names for your imported objects.
Please see the MDN Guide JavaScript modules.