Skip to content

Instantly share code, notes, and snippets.

@elyosemite
Created March 7, 2018 02:23
Show Gist options
  • Select an option

  • Save elyosemite/dc40c6343d4bf1a41fad53c90cc2c853 to your computer and use it in GitHub Desktop.

Select an option

Save elyosemite/dc40c6343d4bf1a41fad53c90cc2c853 to your computer and use it in GitHub Desktop.
var myModule = require('myModule');
var myModuleInstance = new myModule();
myModule.hello();
myModule.goodbye();
@melodevbr
Copy link

you should do this var myModule = require('./func.js')

@elyosemite
Copy link
Author

Hey there, it says that myModule.hello is not a function, look at:
myModule.hello();
^

TypeError: myModule.hello is not a function
at Object. (/home/yuri/app.js:4:10)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

@melodevbr
Copy link

Oh man! πŸ˜‚ You're calling that thing in the wrong place

myModule is the class
myModuleInstance is the right instance where you can call your methods

@elyosemite
Copy link
Author

Well, both func.js and app.js are at the same place.

@melodevbr
Copy link

read what I wrote, I said the solution for you, in any moment I talked about where the files are

@elyosemite
Copy link
Author

I got it man.
It worked out.
Thank you very much!

@melodevbr
Copy link

πŸŽ‰ πŸŽ‰ πŸŽ‰

@elyosemite
Copy link
Author

elyosemite commented Mar 7, 2018

It worked out, but doesn't how say in this article: https://medium.freecodecamp.org/javascript-modules-a-beginner-s-guide-783f7d7a5fcc [PART - CommonJS].
I was trying to understand the CommonJS module, where it has put the path without ./.
Because of that I put var myModule = require('func.js'); without ./

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment