0

I`m trying to get nowjs to work with express 3.0 just like in this question. I am following the code but I still get "Now is not defined".

I am running Ubuntu 12.04 in an Oracle VM virtual box.

First I create an express app.

$ express myBrilliantNewApp

Then I install nowjs in the new dir.

$ sudo npm install now

Add the server variable and use it with now.

var server = http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
})

var everyone = require('now').initialize(server);

Then I should be able to do this...

everyone.now.writesomething = function() {
console.log('I would really like it if this shows up at the console') 
};

...and make the call and check what everyone is all about.

now.writesomething();
console.log(everyone);

It does not work. I get:

[ReferenceError: now is not defined]
ReferenceError: now is not defined
at Object.<anonymous> (/home/jan/dev/apps/probeer1/app.js:44:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Can anyone tell me what I am doing wrong here?

Full listing app.js

/**
* Module dependencies.
*/

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);

var server = http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
})

var everyone = require('now').initialize(server);


everyone.now.writesomething = function() {
  console.log('I would really like it if this shows up at the console')
};

now.writesomething();
console.log(everyone);
Community
  • 1
  • 1
Mr.
  • 51
  • 5

2 Answers2

0

Before using nowjs on the server you need to call:

var nowjs = require('now');

To use nowjs on the client you need to include a script reference in your HTML:

<script src="/nowjs/now.js"></script>
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • thnx, my bad when tinkering with the code I forgot. Put it back in. Same (kind of) problem however – Mr. Aug 14 '12 at 05:28
  • Did you include the now.js script reference on the client side? See updated answer. – JohnnyHK Aug 14 '12 at 15:20
  • I tried to make a clean reproduction of the error, calling a function from server side. See updated question. Should this work? – Mr. Aug 14 '12 at 21:41
  • Also tried a client side call (now.js reference included). Now.js script loads. I get the error "now.writesomething is not a function" in the client console. – Mr. Aug 14 '12 at 22:05
0

See this answer for details: Cannot find module 'now' - nowjs and nodejs

npm config set global true && \
     echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc && \
     . ~/.bashrc

Try again with global flag

sudo npm install now -g
Community
  • 1
  • 1
  • This seems indeed to be the problem although NODE_PATH variable seems ok. echo $NODE_PATH => /usr/local/lib/node_modules. Problem persists. I get, Checking for node path : not found, when installing with npm. I get the same error when trying dnode for example. – Mr. Aug 16 '12 at 07:04
  • I have tried both global and local already. Doesn't work. Maybe something with the user scope of the NODE_PATH env variable? – Mr. Aug 16 '12 at 11:51