Node command isn't doing anything - node.js

On my linode server that I have SSH'd into, when I type
$ node server.js
$
Nothing happens!
Even if the only contents of sever.js is util.log("HELLO");
Nothing is printed! Why is this happening?

Related

Node.js just returns "..." in terminal

I'm a front end dev trying to learn and get into Node.js
Having trouble at the first hurdle. I have an app.js file in the root of Mac: "/"
app.js has the following:
console.log('hello world!');
Whenever I run node app.js in the terminal I get the following:
> node app.js
...
It looks like you're running the Node.js REPL first. Don't do that... the Node.js REPL is for running immediate evaluations.
Just try running node app.js from your normal system terminal.

node.exe does not read file, but returns ellipsis (...)

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?
You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.
It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

how to debug nodeunit using node-inspector

I can do:
I can test node.js modules using nodeunit.
I can debug my node.js express site using node inspector.
But how to debug nodeunit test using node inspector?
I tried, but not working:
nodeunit --debug myNodeUnitModule_test.js It's not working.
I tried to install nodebug.
And used it like this: nodebug /usr/local/bin/nodeunit myNodeunit_test.js But it's not working neither on ubuntu (No such file or directory) nor on mac (env: node\r: No such file or directory)
Almost works
node --debug /usr/local/bin/nodeunit ./routes/edit/bodyTelInfoArraysToObject_test.js
where /usr/local/bin/nodeunit is path taken by command which nodeunit
got output:
debugger listening on port 5858
and test executed there.
But I can't jump in debuggin: when I open url localhost:8080 in chrome to watch debugging:
first load I see empty file list
second load: page not found.
On my nodeunit test I wrote debugger to stop on debug there.
But nothing.
In your tests insert debugger; command
exports['Main test'] = function(test){
debugger;
test.expect(1);
test.ok(true, 'Must be ok');
test.done();
};
And start all this
$ node --debug-brk `which nodeunit` test.js
Now in browser press F8, then F10, and you are right on the next line after first debugger; command in your test.
But I prefer to start everything with node-supervisor, that restart test automatically when test finished or files in project directory changed:
$ npm -g install supervisor node-inspector
$ # console 1
$ # supervisor restarts node-inspector when it quits
$ # ignores file changes
$ supervisor -i . -x node-inspector .
$ # console 2
$ supervisor --debug-brk -- `which nodeunit` test/index.js
Solution found:
in console:
node --debug-brk `which nodeunit` ./path/To/My/NodeUnitTests/nodeunit_test.coffee (Attention: `which nodeunit` is in back quotes)
in another console:
node-inspector &
And in google chrome open: http://0.0.0.0:8080/debug?port=5858
Here I see nodeunit debuging from the start. Click continue execution several times in browser until jump to nodeunit test, where I have debugger; string. So I debugging my nodeunit test with nodeinspector

Node.js forever with environment variable

The command I run on my server to start my node app is:
sudo IS_PROD=1 node app.js
I have forever installed but can't seem to pass in the environment variable.
sudo IS_PROD=1 forever node app.js
Doesn't seem to do the trick. I have tried several varieties of this. How do I either execute this command successfully or permanently set the environment variable?
First of all you should skip the node thing in you command, it should not be there, you should not be able to execute that. forever automatically starts your script using nodejs. Instead you should do like this;
sudo IS_PROD=1 forever app.js
Probably you, instead of starting your server in foreground, will want to start your server as a daemon. eg.
sudo IS_PROD=1 forever start app.js
This will create a process in the background that will watch your node app and restart it when it exits. For more information see the readme.
Both of these methods preserves the environment variables, just like when you are just using node.
app.js:
console.log(process.env.IS_PROD);
Using node (v0.8.21)
$ node app.js
undefined
$ IS_PROD=1 node app.js
1
$ sudo IS_PROD=1 node app.js
1
Using forever (v0.10.0)
$ forever app.js
undefined
$ IS_PROD=1 forever app.js
1
$ sudo IS_PROD=1 forever app.js
1
Documentation:
process.env
An object containing the user environment. See environ(7).

Having trouble running Phantomjs + node on Heroku

I've successfully got Phantomjs to work on Heroku but now I'm hitting issues with the phantomjs-node interface for node.js (see https://github.com/sgentle/phantomjs-node).
When I'm attempting to initialize Phantom I'm seeing a 10-15 second delay and then:
> phantom stdout: ReferenceError: Can't find variable: socket
phantom stdout: phantomjs://webpage.evaluate():1
phantomjs://webpage.evaluate():1
phantomjs://webpage.evaluate():1
You can reproduce the problem with the following steps or by pulling down my test app at https://github.com/matellis/phantom-test
git init phantom-test
cd phantom-test
heroku apps:create
# create node app as per Heroku instructions here https://devcenter.heroku.com/articles/nodejs
# copy bin and lib folders from http://phantomjs.googlecode.com/files/phantomjs-1.6.1-linux-x86_64-dynamic.tar.bz2 into root of your new project
# if you don't do this step you'll get an error "phantom stderr: execvp(): No such file or directory"
git add .
git commit -m "init"
git push heroku
Test your app has come up, the third to last line will tell you the URL, it should read like:
http://fathomless-ravine-5563.herokuapp.com deployed to Heroku
If successful you should see Hello World! in your browser.
Now from the same folder as your Heroku application run:
heroku run node
At the node prompt try the following:
phantom = require('phantom');
x = phantom.create();
Wait 10-15 seconds and you should see the error. Nothing works from this point on.
This should output file foo.png:
x = phantom.create(function(ph){ph.createPage(function(page){ page.open('http://bbcnews.com', function(status){ page.render('foo.png', function(result) {ph.exit()}); }); }); });
To validate Phantomjs is working fine on Heroku, try the following using my test project:
>heroku run bash
Running `bash` attached to terminal... up, run.1
~ $ phantomjs test.js http://bbcnews.com foo.png
~ $ ls *.png
foo.png
I cannot reproduce any of these problems locally but there are other issues reported where folks may have hit this issue locally.
The problem seems to originate in shim.js line 1637:
s.on('request', function(req) {
var evil;
evil = "function(){socket.emit('message', " + (JSON.stringify(JSON.stringify(req))) + " + '\\n');}";
return controlPage.evaluate(evil);
});
I've tried variations of versions of node, phantom, etc. with no luck.
I've also tried a custom buildpack that sets the DYLD variable, see http://github.com/tecnh/heroku-buildpack-nodejs with no luck either.
Anyone who has got Phantom + Node playing together nicely on Heroku please let me know. There are several references to this on Stackoverflow but nobody is saying "I got it to work, here's how".
I've never used the phantomjs node module, but I do have an app running both node and phantomjs on Heroku.
You need to use custom buildpacks in order to get this to work. My .buildpacks file looks like
http://github.com/heroku/heroku-buildpack-nodejs.git
http://github.com/stomita/heroku-buildpack-phantomjs.git
You should then be able to run phantomjs scripts in a child process:
var script = app.get('root') + '/scripts/rasterize.js' //the phantomjs script to run
, bin = app.get('phantom') //normally this would just be the string "phantomjs"
, spawn = require('child_process').spawn;
// set up args to the phantom cli
// (run the phantomjs command in your terminal to see options/format)
var args = [];
// ...
var phntm = spawn(bin, args);
phntm.stdout.on('data', function (data) { /* do something */ });
phntm.stderr.on('data', function (data) { /* do something */ });
phntm.on('exit', function (code) { /* handle exit */ });
Heroku does not support WebSockets. With Socket.io it has a workaround. Not sure about dnode, which phantomjs-node uses.
I had a problem with WebSockets on Heroku too and I switched to Nodejitsu, which solved it for me.

Resources