-2

I have an Errors class that I'm exporting using module.export.

When I require the class in another file using const Errors = require('errors.js'); and then try and use throw Errors.NotImplimented I get an undefined error at the start of throw.

If I try and console.log the Errors class after requiring it I'm shown an empty object.

'use strict';

class Errors {
    NotImplimented() {
        return new Error('Not implimented');
    }
    HTTP_500() {
        return new Error('500 Internal Server Error');
    }
    HTTP_404() {
        return new Error('404 Page Not Found');
    }
}

module.export = Errors;
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
  • You should not use a `class` for something that has no instance state. Just use a plain object literal. – Bergi Jun 07 '17 at 12:44

1 Answers1

3

Two problems. One, it's not exporting. Try module.exports = Errors;

Two, you're not creating an instance of the class. Try const errors = new Errors();

Additionally (not the case with this problem), you may also be getting an empty object because of a circular require, more here.

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
D. Walsh
  • 1,963
  • 1
  • 21
  • 23
  • Exact same thing. From what I know export is fine if you're only exporting a single var, object, etc. and you want the exported item to be the default. Since everything is inside the Errors Class that shouldn't change anything. – Alexis Tyler Feb 24 '17 at 06:42
  • Calling a class like it's a function will change things. It must be instantiated first. – D. Walsh Feb 24 '17 at 06:43