You can simply do this:
user.js
class User {
server.js
const User = require('./user.js')
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.
Paul Rumkin Aug 09 2018-11-11T00: 00Z
source share