How to export ES6 class to Node 4?

I defined the class in the module:

"use strict"; var AspectTypeModule = function() {}; module.exports = AspectTypeModule; var AspectType = class AspectType { // ... }; module.export.AspectType = AspectType; 

But I get the following error message:

 TypeError: Cannot set property 'AspectType' of undefined at Object.<anonymous> (...\AspectType.js:30:26) at Module._compile (module.js:434:26) .... 

How should I export this class and use it in another module? I saw other SO questions, but I get other error messages when I try to implement their solutions.

+83
javascript module export
Sep 18 '15 at 17:08
source share
10 answers

If you use ES6 in Node 4, you cannot use the ES6 module syntax without a transpiler, but CommonJS modules (Node standard modules) work the same way.

 module.export.AspectType 

it should be

 module.exports.AspectType 

therefore, the error message “Unable to set the AspectType property from undefined” because module.export === undefined .

Also for

 var AspectType = class AspectType { // ... }; 

can you just write

 class AspectType { // ... } 

and get essentially the same behavior.

+97
Sep 18 '15 at 17:40
source share
 // person.js 'use strict'; module.exports = class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } display() { console.log(this.firstName + " " + this.lastName); } } 

 // index.js 'use strict'; var Person = require('./person.js'); var someone = new Person("First name", "Last name"); someone.display(); 
+95
Mar 09 '16 at 19:22
source share

With ECMAScript 2015, you can export and import several classes like this

 class Person { constructor() { this.type = "Person"; } } class Animal{ constructor() { this.type = "Animal"; } } module.exports = { Person, Animal }; 

then where do you use them:

 const { Animal, Person } = require("classes"); const animal = new Animal(); const person = new Person(); 

In case of name conflicts or you prefer other names, you can rename them as follows:

 const { Animal : OtherAnimal, Person : OtherPerson} = require("./classes"); const animal = new OtherAnimal(); const person = new OtherPerson(); 
+29
Mar 02 '18 at 20:45
source share

Using

 // aspect-type.js class AspectType { } export default AspectType; 

Then to import it

 // some-other-file.js import AspectType from './aspect-type'; 

Read more at http://babeljs.io/docs/learn-es2015/#modules

+13
Sep 18 '15 at 17:12
source share

class expression can be used for simplicity.

  // Foo.js 'use strict'; // export default class Foo {} module.exports = class Foo {} 

-

 // main.js 'use strict'; const Foo = require('./Foo.js'); let Bar = new class extends Foo { constructor() { super(); this.name = 'bar'; } } console.log(Bar.name); 
+11
Dec 07 '15 at 9:13
source share

Several other answers have come close, but to be honest, I think you better go with the simplest and simplest syntax. The OP requested a class export facility in ES6 / ES2015. I do not think you can become much cleaner than this:

 'use strict'; export default class ClassName { constructor () { } } 
+8
Jan 25 '17 at 18:11
source share

I just write it like this

in the AspectType file:

 class AspectType { //blah blah } module.exports = AspectType; 

and import it as follows:

 const AspectType = require('./AspectType'); var aspectType = new AspectType; 
+8
Oct. 14 '17 at 9:21 on
source share

I had the same problem. What I found, I named my receiving object with the same name as the class name. Example:

 const AspectType = new AspectType(); 

this little thing ... hope this helps

0
Sep 21 '16 at 12:50
source share

ReferenceError: The module is not defined in Object. (/ Users / Shared / MAKE FIRST CODING / 0 SPESTING SPACE / HTML course / scripts / rectangle.js: 12: 1) in Module._compile (module.js: 653: 30) in Object.Module._extensions..js ( module.js: 664: 10) in Module.load (module.js: 566: 32) in tryModuleLoad (module.js: 506: 12) in Function.Module._load (module.js: 498: 3) in the module. require (module.js: 597: 17) on demand (internal /module.js: 11:18)

the same thing happened to me, but the problem is not solved .. I am trying to export the Rectangle class to a node, but the visual code offers a quick solution for converting it to ES6. module.exports = Rectangle; became the default export rectangle; Then, using the command on the node "var Rectangle = require ('./rectangle.js')", the following syntax error appears: Unexpected token export

0
Jul 19 '19 at 7:42
source share

Sometimes I need to declare several classes in one file, or I want to export the base classes and keep their names exported because the JetBrains editor understands this better. I just use

 global.MyClass = class MyClass { ... }; 

And somewhere else:

 require('baseclasses.js'); class MySubclass extends MyClass() { ... } 
-one
May 15 '17 at 14:09
source share



All Articles