admin管理员组文章数量:1023744
I want to export a class with static methods in a module, along with other functions. I'm trying to do
module.exports = {
fun: function(){},
class: MyClass
}
class MyClass {
static get prop() {
return 'property';
}
}
But it does not work. Is there a way to export class as part of module.exports object?
I want to export a class with static methods in a module, along with other functions. I'm trying to do
module.exports = {
fun: function(){},
class: MyClass
}
class MyClass {
static get prop() {
return 'property';
}
}
But it does not work. Is there a way to export class as part of module.exports object?
Share Improve this question edited Mar 2, 2016 at 10:33 Anton asked Mar 2, 2016 at 10:10 AntonAnton 451 silver badge9 bronze badges 5-
I'm using not
class
but sayabc
and it's not working -- the error is thatabc
is not defined when I try to require it from other file. I can do module.exports.abc = MyClass, but that would not allow me to use justMyClass
inside the module – Anton Commented Mar 2, 2016 at 10:19 -
I see
myClass
and notMyClass
in your exports definition – Backslash36 Commented Mar 2, 2016 at 10:20 -
Why don't you use ES6 module exports?
export class MyClass {} export function fun() {}
– Bergi Commented Mar 2, 2016 at 10:42 - @Bergi: ES6 module imports can only be on the top of the file. Sometimes it's necessary to conditional import a module. This is not possible then. – andreas Commented Jul 8, 2017 at 17:11
-
@andreas It will be possible with
import()
statements, and until then you can still justrequire
the transpiled module – Bergi Commented Jul 8, 2017 at 18:25
1 Answer
Reset to default 5Class definitions aren't hoisted, which means that your class won't be in scope when you declare those exports. Move them down to below the definition.
class MyClass {
static get prop() {
return 'property';
}
}
module.exports = {
fun: function(){},
class: myClass
}
You'll also need to fix the case on the variable you export.
module.exports = {
fun: function(){},
class: MyClass
}
Depending on your Javascript environment, there may be pile time errors if you try and used the reserved word class
as a literal object property. You can wrap it in a string to avoid this.
module.exports = {
fun: function(){},
"class": MyClass
}
I want to export a class with static methods in a module, along with other functions. I'm trying to do
module.exports = {
fun: function(){},
class: MyClass
}
class MyClass {
static get prop() {
return 'property';
}
}
But it does not work. Is there a way to export class as part of module.exports object?
I want to export a class with static methods in a module, along with other functions. I'm trying to do
module.exports = {
fun: function(){},
class: MyClass
}
class MyClass {
static get prop() {
return 'property';
}
}
But it does not work. Is there a way to export class as part of module.exports object?
Share Improve this question edited Mar 2, 2016 at 10:33 Anton asked Mar 2, 2016 at 10:10 AntonAnton 451 silver badge9 bronze badges 5-
I'm using not
class
but sayabc
and it's not working -- the error is thatabc
is not defined when I try to require it from other file. I can do module.exports.abc = MyClass, but that would not allow me to use justMyClass
inside the module – Anton Commented Mar 2, 2016 at 10:19 -
I see
myClass
and notMyClass
in your exports definition – Backslash36 Commented Mar 2, 2016 at 10:20 -
Why don't you use ES6 module exports?
export class MyClass {} export function fun() {}
– Bergi Commented Mar 2, 2016 at 10:42 - @Bergi: ES6 module imports can only be on the top of the file. Sometimes it's necessary to conditional import a module. This is not possible then. – andreas Commented Jul 8, 2017 at 17:11
-
@andreas It will be possible with
import()
statements, and until then you can still justrequire
the transpiled module – Bergi Commented Jul 8, 2017 at 18:25
1 Answer
Reset to default 5Class definitions aren't hoisted, which means that your class won't be in scope when you declare those exports. Move them down to below the definition.
class MyClass {
static get prop() {
return 'property';
}
}
module.exports = {
fun: function(){},
class: myClass
}
You'll also need to fix the case on the variable you export.
module.exports = {
fun: function(){},
class: MyClass
}
Depending on your Javascript environment, there may be pile time errors if you try and used the reserved word class
as a literal object property. You can wrap it in a string to avoid this.
module.exports = {
fun: function(){},
"class": MyClass
}
本文标签: javascriptExport a class in Node moduleexportsStack Overflow
版权声明:本文标题:javascript - Export a class in Node module.exports = {} - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745545126a2155358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论