omarwaleed
omarwaleed

Reputation: 681

Running JavaScript on Ubuntu terminal gives no output

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.

Upvotes: 1

Views: 2054

Answers (2)

omarwaleed
omarwaleed

Reputation: 681

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.

Upvotes: 1

user93
user93

Reputation: 1866

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();

Upvotes: 4

Related Questions