admin管理员组文章数量:1294321
I have very simple beginner problem using node.js but I cannot seem to get it to work correctly.
All I want is to be able to create objects using the 'new' operator in the index.js file.
For demonstration purposes, I have created a simple Person
object inside a Person.js file (which lies in the same directory as my index.js) like this:
class Person {
constructor() {
this._name = 'bob';
}
set name(name) {
this._name = name
}
get name() {
return this._name;
}
}
Now I want to do something like this (in the index.js file):
var p = require('./Person.js')
var bob = new p.Person()
The error message I get is telling me that Person is not a constructor:
var bob = new p.Person()
^
TypeError: p.Person is not a constructor
at Object.<anonymous> (/home/.../index.js:59:11)
at Module._pile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Any help is greatly appreciated.
I have very simple beginner problem using node.js but I cannot seem to get it to work correctly.
All I want is to be able to create objects using the 'new' operator in the index.js file.
For demonstration purposes, I have created a simple Person
object inside a Person.js file (which lies in the same directory as my index.js) like this:
class Person {
constructor() {
this._name = 'bob';
}
set name(name) {
this._name = name
}
get name() {
return this._name;
}
}
Now I want to do something like this (in the index.js file):
var p = require('./Person.js')
var bob = new p.Person()
The error message I get is telling me that Person is not a constructor:
var bob = new p.Person()
^
TypeError: p.Person is not a constructor
at Object.<anonymous> (/home/.../index.js:59:11)
at Module._pile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Any help is greatly appreciated.
Share Improve this question edited Aug 4, 2018 at 19:31 Florian Baierl asked Aug 4, 2018 at 19:11 Florian BaierlFlorian Baierl 2,4914 gold badges27 silver badges53 bronze badges 1- 1 You can only import something if you exported it already. – Jonas Wilms Commented Aug 4, 2018 at 19:18
1 Answer
Reset to default 8If you want to require or import class/function/object/anything from another JS file, you need to export that and import/require that in your current file. These are called modules.
In your case, You need to export the Person
class after creating. Otherwise JS doesn't have any reference to it. Add
module.exports = Person
below the Person
class. Now you can instantiate by
const Person = require('./person');
const p = new Person();
For the new ES6 classes you can use import/export
as well.
// Export Person Class
export default class Person {
....
}
// Now, import
import Person from './person'
本文标签: javascriptUse requirenew to instantiate a new object in nodejsStack Overflow
版权声明:本文标题:javascript - Use requirenew to instantiate a new object in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741594009a2387301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论