如何在Node 4中正确导出ES6类?

Jér*_*nge 96 javascript module export node.js

我在模块中定义了一个类:

"use strict";

var AspectTypeModule = function() {};
module.exports = AspectTypeModule;

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

module.export.AspectType = AspectType;
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误消息:

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

我该如何导出这个类并在另一个模块中使用它?我已经看到了其他SO问题,但是当我尝试实现他们的解决方案时,我收到了其他错误消息.

log*_*yth 104

如果您在节点4中使用ES6,则不能在没有转换器的情况下使用ES6模块语法,但CommonJS模块(Node的标准模块)的工作方式相同.

module.export.AspectType
Run Code Online (Sandbox Code Playgroud)

应该

module.exports.AspectType
Run Code Online (Sandbox Code Playgroud)

因此错误消息"无法设置属性'AspectType'未定义"因为module.export === undefined.

另外,为

var AspectType = class AspectType {
    // ...    
};
Run Code Online (Sandbox Code Playgroud)

你能写一下吗?

class AspectType {
    // ...    
}
Run Code Online (Sandbox Code Playgroud)

并获得基本相同的行为.

  • OMG `export` instead of `exports`, how did I miss that? (20认同)
  • 最后我放了`module.exports = ClassName`,它工作正常 (4认同)

小智 99

// person.js
'use strict';

module.exports = class Person {
   constructor(firstName, lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }

   display() {
       console.log(this.firstName + " " + this.lastName);
   }
}
Run Code Online (Sandbox Code Playgroud)

 

// index.js
'use strict';

var Person = require('./person.js');

var someone = new Person("First name", "Last name");
someone.display();
Run Code Online (Sandbox Code Playgroud)

  • @sitrakay,您应该真正添加一个有关如何解决此问题的说明。 (2认同)

Jon*_*del 36

使用ECMAScript 2015,您可以导出和导入多个类

class Person
{
    constructor()
    {
        this.type = "Person";
    }
}

class Animal{
    constructor()
    {
        this.type = "Animal";
    }
}

module.exports = {
    Person,
    Animal
};
Run Code Online (Sandbox Code Playgroud)

然后你在哪里使用它们:

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

const animal = new Animal();
const person = new Person();
Run Code Online (Sandbox Code Playgroud)

如果出现名称冲突,或者您更喜欢其他名称,可以像下面这样重命名:

const { Animal : OtherAnimal, Person : OtherPerson} = require("./classes");

const animal = new OtherAnimal();
const person = new OtherPerson();
Run Code Online (Sandbox Code Playgroud)

  • 错误的。原因:如果您在 Node 4 中使用 ES6,则在没有转译器的情况下无法使用 ES6 模块语法,但 CommonJS 模块(Node 的标准模块)的工作原理相同。(如上所述) (4认同)

Tha*_*you 14

使用

// aspect-type.js
class AspectType {

}

export default AspectType;
Run Code Online (Sandbox Code Playgroud)

然后导入它

// some-other-file.js
import AspectType from './aspect-type';
Run Code Online (Sandbox Code Playgroud)

阅读http://babeljs.io/docs/learn-es2015/#modules了解更多详情

  • 导出和导入尚未在节点使用的V8中实现.你仍然需要使用`module.exports` (9认同)
  • ......或者说是变性(即巴贝尔).NodeJS具有大多数ES6功能.. _excluding_`import` /`export`(仍然适用,2017年5月). (2认同)

mas*_*tic 12

类表达式可以用于简单.

 // Foo.js
'use strict';

// export default class Foo {}
module.exports = class Foo {}
Run Code Online (Sandbox Code Playgroud)

-

// main.js
'use strict';

const Foo = require('./Foo.js');

let Bar = new class extends Foo {
  constructor() {
    super();
    this.name = 'bar';
  }
}

console.log(Bar.name);
Run Code Online (Sandbox Code Playgroud)

  • 只是一个警告,在Node中这取决于模块加载顺序.所以要小心使用它.如果你切换这些文件的名称周围的例子将无法正常工作. (4认同)

小智 11

我只是这样写

在AspectType文件中:

class AspectType {
  //blah blah
}
module.exports = AspectType;
Run Code Online (Sandbox Code Playgroud)

并像这样导入:

const AspectType = require('./AspectType');
var aspectType = new AspectType;
Run Code Online (Sandbox Code Playgroud)


Cra*_*tes 8

其他几个答案很接近,但老实说,我认为你最好采用最干净,最简单的语法.OP要求在ES6/ES2015中输出一个类.我认为你不能比这更清洁:

'use strict';

export default class ClassName {
  constructor () {
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 错误.原因:如果您在节点4中使用ES6,则无法在没有转换器的情况下使用ES6模块语法,但CommonJS模块(节点的标准模块)的工作方式相同.(如上所述) (2认同)
  • 谁还在使用Node 4?我认为这是99%的人的有效答案. (2认同)
  • 从字面上看,它就在问题的标题中。 (2认同)