flatironjs and Cloud9; window is undefined error? - node.js

Anyone else having luck using flatironjs with the Cloud9 ide?
In my server.js file I have:
require("coffee-script");
var app = require("./app");
app.listen(process.env.PORT);
Then in my app.coffee file I have:
flatiron = require "flatiron"
director = require "director"
app = flatiron.app
app.use flatiron.plugins.http
module.exports = app.router.get "/", ->
res.writeHead 200, { "Content-Type": "text/plain" }
res.end "Hello world!\n"
When I attempt to run this in the Cloud9 IDE I get the following:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
**^ ReferenceError: window is not defined**
at Object. (/node_modules/flatiron/node_modules/broadway/node_modules/eventemitter2/lib/eventemitter2.js:547:63)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at require (module.js:355:19)
at Object. (/node_modules/flatiron/node_modules/broadway/lib/broadway/app.js:11:14)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
If I create a standard http server without using flatiron everything runs great:
http = require "http"
module.exports = http.createServer (req, res) ->
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end "Hello World\n"
Thoughts?

This is a bug in EventEmitter running on Solaris. You can see it as well if you just run an app on the latest version of Solaris, will crash with the same error message. You can use the patched EventEmitter2 that removes the check for the browser.
I created an issue for you.

So there's some code at the bottom of that eventemitter2.js file that basically tries to be "isomorphic" and work in both node.js and a browser. It tries to guess which environment by testing for the following global variables being defined:
process
process.title
exports
If all of those are defined, eventemitter2 will attach it's exported properties to the exports object for use in node.js. Otherwise, it will attach them to the window object for use in a browser.
For some reason inside cloud9, 1 or more of those 3 global variables is not defined, and it's branching to "browser" mode assuming window is there and failing. I don't know enough about the cloud9 ide hosting environment to understand exactly which one (or 2 or 3) of them it is and why it's missing.
Your vanilla http code works because it doesn`t load eventemitter2, which IS loaded when you use flatiron, which depends on broadway, which depends on eventemitter2.

Related

Firebase NodeJs ReferenceError: Promise is not defined

I came across this error message when trying to deploy a firebase node application to a virtual private server:
/home/.../Backend/node_modules/firebase-admin/lib/firebase-namespace.js:195
this.Promise = Promise;
^
ReferenceError: Promise is not defined
at new FirebaseNamespace (/home/.../Backend/node_modules/firebase-admin/lib/firebase-namespace.js:195:24)
at Object.<anonymous> (/home/.../Backend/node_modules/firebase-admin/lib/default-namespace.js:5:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/.../Backend/node_modules/firebase-admin/lib/index.js:4:16)
at Module._compile (module.js:456:26)
On my local environment, this node application runs without any problem. Both environments are having the same node, npm, and "firebase-admin" module version.
So, I followed the suggestion from here and modified the "firebase-admin" module files on the virtual server. By adding
var Promise = require('es6-promise').Promise;
to some of the module source files manually, I can get rid of the error messages. After that, nothing can be read from the firebase database.
My code section
firebaseDatabase.ref("...").once('value').then(function(snapshot){
....
});
which reads the contents of firebase with no problem on my local environment, never have its "then" called on the virtual server.
What am I doing wrong? Any suggestion is appreciated.
I manage to solved the problem. Just in case if anyone run into the same issue, here are the steps how I fixed it:
For my case, I removed all the modifications I made to the firebase-admin module.
install "es6-promise" if you haven't. (npm install es6-promise --save)
Add the following line to your "server.js" file:
require('es6-promise').polyfill();
Notice that we don't assign the result of polyfill() to any variable. The polyfill() method will patch the global environment (in this case to the Promise name) when called.
I encountered this issue as soon as I firebase init. I did not change or added any codes in the generated scripts. I did solve the issue and was able to deploy by:
Going to the functions folder "cd functions"
sudo npm install es6-promise --save
Browse to the functions/node_modules/firebase-admin/lib/firebase-namespace.js
Add this on top
var Promise = require('es6-promise').Promise;
Your header should look like this:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var deep_copy_1 = require("./utils/deep-copy");
var error_1 = require("./utils/error");
var firebase_app_1 = require("./firebase-app");
var credential_1 = require("./auth/credential");
var DEFAULT_APP_NAME = '[DEFAULT]';
var globalAppDefaultCred;
var globalCertCreds = {};
var globalRefreshTokenCreds = {};
var Promise = require('es6-promise').Promise;

how can add a new view / route into express?

I'm new at this, I'm trying to absorb as much as possible.
I am using this template https://github.com/primaryobjects/Node.js-Bootstrap-Starter-Template
Okay, all right till here, but when I try to add a new page, it returns me the following error:
C:\server\node_modules\express\lib\router\route.js:196
throw new Error(msg);
^
Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (C:\server\node_modules\express\lib\r
outer\route.js:196:15)
at EventEmitter.app.(anonymous function) (C:\server\node_modules\express\lib
\application.js:481:19)
at Object.<anonymous> (C:\server\app.js:31:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
The aim would be to answer me a new page at the following address:
http://localhost:3000/ask?america=
The steps I followed were:
Create a view and a path
/views/ask.jade
extends layout
block content
div.container
h1 ASK
2n
/routes/ask.js
exports.ask= function(req, res){
res.render('ask');
};
3r
app.js
app.get('/ask', routes.ask);
But when I try to start "node app" returns me the error mentioned above.
Where am I mistaken?
Have you tried requiring specifically your routes/ask.js file? I'm pretty certain when using
require('folder')
require looks directly for an index file and uses that index file which I believe may mean that your routes/ask.js file might actually not be getting "required" by the app.
So
var askRoutes = require('./routes/ask')
...
...
app.get('/', routes),
app.get('/ask', askRoutes)
To modularize your code you could utilize your routes/index.js file as a routes module load file, where you load your route modules (like routes/ask.js) into your index file so you won't get bogged down by having a ton of required route modules in your app.js file. Just the index.js file would be required.
Can you try the followings :
use the default route first : app.get('/', routes.index); and then put your code after app.get('/ask', routes.ask);
if it doesn't work, put instead of res.render('ask'); the minimal return res.send('some json');

Node.js TypeError: object is not a function

I'm trying to run Mike Wilson's book sample app, but receiving the following error:
pcassiano#...:~/socialnet$ sudo node app.js
/home/pcassiano/socialnet/app.js:60
require('./routes/' + routeName)(app, models);
^
TypeError: object is not a function
at /home/pcassiano/socialnet/app.js:60:35
at Array.forEach (native)
at Object.<anonymous> (/home/pcassiano/socialnet/app.js:57:26)
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)
i'm running the latest code from the book's repo.
what should i do in order to run this sample app properly?
thanks in advance.
You likely have .js files in the routes directory that do not export a function.
app.js is calling require on all files in the routes directory and then calling them as a function. So if any of those files do not follow the general pattern below you'll get the error you're seeing:
module.exports = function(app, models) {
// Add this file's routes using app.get, etc. calls.
...
};
The error reports that the value of require('./routes/' + routeName) is not a function. So you have only one real possible source of the problem.
If './routes/'+routeName doesn't exist, node should throw an error (in v0.8.16 at least), so that isn't it. So the most obvious one is that the module being loaded doesn't export a function (as the error suggests).
You should run console.log('routeName: ', routeName) right before the require statement that is on line 60 of app.js and try running it again. Then, after finding the value of routeName, look in the file it's trying to open. Odds are either module.exports is either exporting an object (or an array, string etc), or is not set at all.
If you're adding routes in the routes folder, take care to follow the same exporting style as the author, namely module.exports = function(app, models) {...}

Issue with node.js require.paths.unshift()

I'm new to node.js world, I'm using node.js v0.8.4. I googled for this issue. Yes, i got solutions for this, but still could not understand as a beginner. So please help me......
i was going through a node.js tutorial. In that tutorial node.js v0.1.103 is used and he writes
require.paths.unshift(__dirname+'/vendor');
var http = require('http'),
sys = require('sys'),
nodeStatic = require('node-static/lib/node-static');
var server = http.createServer(function(request, response) {
var file = new nodeStatic.Server('./public', function() {
cache: false
});
request.addListener('end', function() {
file.server(request, response);
});
}).listen(8000);
And the script above works for him. When i write the sample script and run, i get this error.
C:\Documents and Settings\local>node C:\xampp\htdocs\rt\node\livestats\
server.js
Error: require.paths is removed. Use node_modules folders, or the NODE_PATH envi
ronment variable instead.
at Function.Module._compile.Object.defineProperty.get (module.js:386:11)
at Object.<anonymous> (C:\xampp\htdocs\rt\node\livestats\server.js:2:8)
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)
How do i make node to load files from custome directory and How to i solve this issue? My OS is windows
Thank you......
require.paths is deprecated a long time ago. The tutorial for node v0.1 is pretty much useless now, as nearly everything has changed since then.
If you installed node-static with npm install, you can just use require("node-static") without specifying the entire path (and, of course, without using require.paths).

Dynamically generate Express routes fails in production environment

I've seen a lot of folks dynaimcally generating all their routes in the thier routes/index.js like so:
require("fs").readdirSync("./routes", 'utf8').forEach(function(file) {
if (file != 'index.js' && file != '.DS_Store') {
require("./"+file);
}
});
This works fine in development but not in production. If I remove this and add the routes manually it works fine. Any Ideas?
Here's my error if you think that would help:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
Error: ENOENT, No such file or directory './routes'
at Object.readdirSync (fs.js:376:18)
at Object.<anonymous> (/app/routes/index.js:4:15)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at require (module.js:346:19)
at Object.<anonymous> (/app/server.js:50:14)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
Process died with exit code 1. Restarting...
As Mark Bessey says in his answer, you are resolving the routes directory from your current directory -- not relative to where your main script lives. You should probably use __dirname. From the docs:
The name of the directory that the currently executing script resides in.
fs.readdirSync(path.join(__dirname, "routes"))
Also, you don't need to pass 'utf8'. Also, be very careful using any Sync functions in your code -- generally, it's ok in the top level scope, before the server starts accepting requests, so it should be ok in this case.
It seems likely that in production, the current directory is not getting set to the parent of your "routes" directory. How are you launching your app in production? What output do you get from
console.log(process.cwd());

Resources