Node.js coffeescript - problems with required modules

I have another problem with node.js, this time I cannot get javascript code to find out that the coffeescript module class has functions.

In my main main.js file, I have the following code:

require('./node_modules/coffee-script/lib/coffee-script/coffee-script'); var Utils = require('./test'); console.log(typeof Utils); console.log(Utils); console.log(typeof Utils.myFunction); 

And in my test.coffe module, I have the following code:

 class MyModule myFunction : () -> console.log("debugging hello world!") module.exports = MyModule 

Here is the result when I run node main.js :

 function [Function: MyModule] undefined 

My question is: why does my main file load the correct module, but why can't it access this function? What am I doing wrong, whether with coffeescript syntax or with how I need my module? Let me know if I clarify my question.

Thanks,

Vineet

+4
source share
1 answer

myFunction is an instance method, so it will not be available directly from the class .

If you want it to be like a class (or static), the @ name prefix to denote the class:

 class MyModule @myFunction : () -> # ... 

You can also export Object if the goal is to have all methods static:

 module.exports = myFunction: () -> # ... 

Otherwise, you need to create an instance, either in main :

 var utils = new Utils(); console.log(typeof utils.myFunction); 

Or, as an export object:

 module.exports = new Utils 
+6
source

Source: https://habr.com/ru/post/1492142/


All Articles