4

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?

7
  • Could be the case sensite of the name of the file, and the name of the class baseModel Commented Jul 26, 2016 at 12:33
  • When you requiresomething, you need to assign the returned object to a name Commented Jul 26, 2016 at 12:37
  • export the class by prefixing export as follows export class BaseModel{} Commented Jul 26, 2016 at 12:39
  • yea you need to use es6 export or es5 modules.export Commented Jul 26, 2016 at 12:41
  • 3
    @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. Commented Jul 26, 2016 at 12:45

1 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

2 Comments

great, works, so how to use es6 export instead modules.export ?
@snaggs: They're not implemented in Node yet. You'd have to use a transpiler such as Babel to use ES6 imports and exports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.