1

I am trying to execute a javascript function on a cloud ubuntu machine. Here's the code:

exports.msg = function(){
    console.log('This is a test function');
}


Now whenever I run this using nodejs on Windows using the command node test.js assuming the filename is test.js it works perfectly and prints the output. But whenever I install nodejs (Version 4.x) on Ubuntu and try running nodejs test.js the console runs with no output what so ever. No errors, no output, no nothing.
Originally I am referring to this link to do this.
I referred to this question but I don't want to use Rhino. I want to use Nodejs. What am I doing wrong.
P.S. I know that nodejs runs using nodejs on Ubuntu and using node on Windows so this is definitely not the problem. Plus if you try running node test.js on Ubuntu without diverting the path of the executable from nodejs it will give an error.
I really don't know what I am missing here. Does nodejs has some log file in Ubuntu that I should check for the output? I tried checking the system log files with no luck.
Thanks.

7
  • 1
    you need to call the function for it to get executed Commented May 11, 2017 at 11:37
  • Can you please tell me a sample command to run? Commented May 11, 2017 at 11:41
  • I am really sorry but I just tried it and it gives an error "msg is not defined" on windows Commented May 11, 2017 at 11:45
  • your code should be var msg = function(){ console.log('This is a test function'); } msg(); Commented May 11, 2017 at 11:49
  • Awesome it works. Please add it as an answer so that i can accept it. Commented May 11, 2017 at 11:51

2 Answers 2

4

your code should be

var msg = function(){ 
  console.log('This is a test function');
}
msg();

to call the function locally define it first as local or you can also do

 exports.msg = function(){
   console.log('This is a test function');
 }
 exports.msg();
Sign up to request clarification or add additional context in comments.

Comments

1

I found another solution that doesn't require changing the code
I used node -e require('./test.js').msg()
The -e evaluates the node environment and requiring the module from it's path and calling the function gives the desired result. This is in case you would like to run an exported function.

Comments

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.