Categories
JavaScript Answers

How to include JavaScript class definition from another file in Node.js?

Spread the love

Sometimes, we want to include JavaScript class definition from another file in Node.js.

In this article, we’ll look at how to include JavaScript class definition from another file in Node.js.

How to include JavaScript class definition from another file in Node.js?

To include JavaScript class definition from another file in Node.js, we can export the class and the import it in another module.

For instance, we write

user.js

export default class User {}

to export the User class as a default export.

Then we write

server.js

import User from "./user";

let user = new User();

to import the User class from user.js and then use it.

Conclusion

To include JavaScript class definition from another file in Node.js, we can export the class and the import it in another module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *