4
votes

I try to learn Node.js (ES6) but fail on require

This is my structure:

enter image description here

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?

1
Could be the case sensite of the name of the file, and the name of the class baseModelÁlvaro Touzón
When you requiresomething, you need to assign the returned object to a namethefourtheye
export the class by prefixing export as follows export class BaseModel{}NishanthSpShetty
yea you need to use es6 export or es5 modules.exportrambossa
@NishanthShetty: Note that they're not using a transpiler, so the ES6 import and export syntax won't work. They have to use require(...) and module.exports.Joe Clay

1 Answers

6
votes

When you define a variable in Node, it isn't added to the global scope like it would be in a browser - it's local to that one file/module. Therefore, you can't simply import a file and expect the things you defined inside it to be available - you explicitly have to export and import them.

BaseModel.js:

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}`)
    }
}

module.exports = BaseModel;

AccountModel.js:

"use strict";

let BaseModel = 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
    }

}

module.exports = AccountModel;

main.js:

"use strict";

let AccountModel = require('./AccountModel.js');

let accounts = new AccountModel(5)

accounts.getName()

console.log('Data is %s', accounts.accountsData);