Including JavaScript class definition from another file in Node.js

I am writing a simple server for Node.js and I am using my own class called User , which looks like this:

 function User(socket) { this.socket = socket; this.nickname = null; /* ... just the typical source code like functions, variables and bugs ... */ this.write = function(object) { this.socket.write(JSON.stringify(object)); } }; 

and then later in the process I create it a lot:

 var server = net.createServer(function (socket) { /* other bugs */ var user = new User(socket); /* more bugs and bad practise */ }); 

Can I transfer the definition of the User class to another javascript file and "include" it somehow?

+93
javascript
Aug 09 '11 at 15:04
source share
6 answers

You can simply do this:

user.js

 class User { //... } module.exports = User 

server.js

 const User = require('./user.js') // Instantiate User: let user = new User() 

UPDATE 2019

Since ES modules have become the standard, some authors use them in their articles, which can be confusing because Node.js does not allow the use of ESM. Meanwhile, Node.js has experimental support and requires a flag for ESM to work. Read about this in the ESM Node.js documentation .

Here is an example of the same behavior for clarification:

user.js

 export default class User {} 

server.js

 import User from './user.js' let user = new User() 

NOTE

Do not use globals, this creates potential conflicts in the future.

+160
Aug 09 2018-11-11T00:
source share

Using ES6, you can have user.js :

 export default class User { constructor() { ... } } 

And then use it in server.js

 const User = require('./user.js').default; const user = new User(); 
+12
Jan 01 '16 at 15:13
source share

Modify the class definition as follows:

 exports.User = function (socket) { ... }; 

Then rename the file to user.js Assuming it is in the root directory of your main script, you can include it as follows:

 var user = require('./user'); var someUser = new user.User(); 

This is a quick and dirty version. Read about CommonJS modules if you want to know more.

+6
Aug 9 2018-11-11T00:
source share

In addition to the ones given here for ES6

 module.exports = class TEST{ constructor(size) { this.map = new MAp(); this.size = size; } get(key) { return this.map.get(key); } length() { return this.map.size; } } 

and turn on the same as

 var TEST= require('./TEST'); var test = new TEST(1); 
+5
Jan 10 '17 at 10:00
source share

If you add this value to user.js :

 exports.User = User; 

then in server.js you can do:

 var userFile = require('./user.js'); var User = userFile.User; 

http://nodejs.org/docs/v0.4.10/api/globals.html#require

Another way:

 global.User = User; 

then this would be enough in server.js :

 require('./user.js'); 
+1
Aug 09 2018-11-11T00:
source share

Instead of myFile.js, write your files as myFile.mjs. This extension comes with all the goodies of ES6, but I mean, I recommend you the web package and Babel

0
Aug 18 '19 at 23:01
source share



All Articles