I try to learn Node.js (ES6) but fail on require
This is my structure:
baseModel.js
"use strict";
class BaseModel {
constructor(options = {}, data = []) { // class constructor
this.name = 'Base'
this.url = 'http://azat.co/api'
this.data = data
this.options = options
}
getName() { // class method
console.log(`Class name: ${this.name}`)
}
}
AccountModel.js
"use strict";
require('./baseModel.js');
class AccountModel extends BaseModel {
constructor(options, data) {
super({private: true}, ['32113123123', '524214691']) //call the parent method with super
this.name += 'Account Model'
this.url +='/accounts/'
}
get accountsData() { //calculated attribute getter
// ... make XHR
return this.data
}
}
main.js
"use strict";
require('./AccountModel.js');
let accounts = new AccountModel(5)
accounts.getName()
console.log('Data is %s', accounts.accountsData);
Now I run: node --harmony-default-parameters main.js
and get error:
ReferenceError: BaseModel is not defined at Object. (/Users/tamirscherzer/POC/projects/NodeJS/tutorials/classes/AccountModel.js:5:28) at Module._compile (module.js:397:26) at Object.Module._extensions..js (module.js:404:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/tamirscherzer/POC/projects/NodeJS/tutorials/classes/main.js:5:1) at Module._compile (module.js:397:26) at Object.Module._extensions..js (module.js:404:10)
Really strange, if I change require('./baseModel.js');
to other name, I get error that file not found so the path is written properly.
Also defined permissions 777
- same thing, BaseModel is not defined
Any Ideas?
require
something, you need to assign the returned object to a name – thefourtheyeexport class BaseModel{}
– NishanthSpShettyexport
or es5modules.export
– rambossaimport
andexport
syntax won't work. They have to userequire(...)
andmodule.exports
. – Joe Clay