0
votes

I am trying to export this class in node.js:

export class className {
  static method1(param1) {
    // do something
  }

  static method2(param1, param2) {
    // do something
  }
}

But it's getting me the following errors in terminal:

complete_path.....\node_modules@babel\runtime\helpers\esm\classCallCheck.js:1 [2] (function (exports, require, module, __filename, __dirname) { export default function _classCallCheck(instance, Constructor) { [2]
^^^^^^ [2] [2] SyntaxError: Unexpected token export [2] at new Script (vm.js:83:7) [2] at createScript (vm.js:267:10) [2] at Object.runInThisContext (vm.js:319:10) [2] at Module._compile (internal/modules/cjs/loader.js:685:28) [2] at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10) [2] at Module.load (internal/modules/cjs/loader.js:620:32) [2]
at tryModuleLoad (internal/modules/cjs/loader.js:560:12) [2] at Function.Module._load (internal/modules/cjs/loader.js:552:3) [2]
at Module.require (internal/modules/cjs/loader.js:658:17) [2] at require (internal/modules/cjs/helpers.js:22:18) [2] [nodemon] app crashed - waiting for file changes before starting...

2

2 Answers

2
votes

Use module.exports and not export

module.exports = class className {

 static method1(param1) {
     // do something
 }


 static method2(param1, param2) {
     // do something
 }

}
2
votes

The export keyword is not supported by Node.js yet. You have to use the exports or module.exports ones.

In your case you should use the module.exports:

module.exports = class className {
  static method1(param1) {
    // do something
  }

  static method2(param1, param2) {
    // do something
  }
}

For more information about the difference between the exports and module.exports I suggest you this post.