Including socket.io.js results in undefined 'require' - node.js

I've just started with node.js and socket.io.
I installed node.js using the windows installer, resulting in the following install path:
C:\Program Files (x86)\nodejs
I used npm to install socket.io and managed to include this in my server file, which is located in my projects folder, under
D:\projects\node\nodeserv.js
I then included the socket.io.js, which is located under the same socket.io folder, under the nodjs folder.
However, when I try to open the HTML file containing the client code, I get an error in socket.io.js stating:
Undefined reference to 'require'
Can someone help me out and see what I am doing wrong?

Make sure to check your spelling very carefully. If you can post post some code to look at, we can probably tell you the problem right away. More than likely it's a simple typo.

It sounds like you are trying to run node.js from the browser. Node.js runs on the server with the node executable. When you open the HTML file in your browser, it will execute the Javascript on it in a non node.js environment.

Apparently you need to get the socket.io.js file from the nodejs server.
So instead of an include, use an async call to get the file, like:
$.getScript('http://localhost:1337/socket.io/socket.io.js', function(){
//You can now use the io namespace
});

Related

Error running node in cloud9 IDE?

First i got the following error and i couldn't figure yet what is this about:
any idea?
The problem is that you're trying to open an HTML file with Node.js.
Node.js is supposed to run JavaScript code (typically stored in .js files) and it cannot parse your HTML file, which is why you're getting these errors.
You could try executing the files called app.js or server.js with the following commands:
node app.js
or
node server.js
Also, it seems your project (or whatever it is you're working on) has a README.md file which could help to clarify where you're supposed to start.

Trying to get MEAN maps to work on windows

I cloned this mean map that I watched on a azure mongodb video , and I did the same steps in the readme like they did
https://github.com/scotch-io/mean-google-maps
So
npm install
node server.js
// I didn't do any mongodb , as there is a config.js that is pointing at an amazon mongodb.
On the video they didn't do any mongodb locally
I see that in chrome console it throws an error with modernizr
#!/usr/bin/env node
That line is red squiggly , it that line causing the map to not load? Is that even going to work on a Windows 10 machine I'm running?
This error has nothing to do with mongodb.
The problem is the Modernizr link in the public/index.html (line 18) points to a script which is designed to be run server side.
The shebang #!/usr/bin/env node indicate a javascript file that must be run with Nodejs. Your browser can't run this kind of script.
It's looks like a confusion in bower dependency management.
(I think it is generaly not a good practive to include bower_components directory into git repository)
Maybe you can try to fix it by replacing the link with a cdnjs version of Modernizer:
https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js
Or just delete the line, you use a recent Chrome browser after all...

Meteor leaderboard app on node-webkit

I am trying to get the meteor leaderboard app to run on Node-Webkit.
I have demeteorized it and compressed it to a .nw file but when I drop it in. I get errors:
Invalid Package There is no 'package.json' in the package, please
make sure the 'package.json' is in the root of the package.
I have read on various thread but nothing clear yet.
It seems like the demeteorized app needs to be restructured. Also need to figure out how to run the server [Locally/DDP].
Edited:
P.S. I am using the demeteorized files from the leaderboard meteor app to be able to run it in node-webkit.
What exactly I am trying to figure out here is :
how to run/init the local node(demeteorized) server and set the port.
How to set environment variables for the demeteorized app for mongodb etc.
What would be used as the
"main": ?,
"node-remote": ?
for the node-webkit package.json file.
Can someone please shed some light and if possible an example will be highly appreciated.
Thanks in advanced.
Praney :)
UPDATE:
After tinkering a bit, I added the "main": "index.html" and added index.html file to the root of the demeteorized app. This file just loads the main.js file in the browser, here:
<!DOCTYPE html>
<html>
<head>
<title>Leaderboards</title>
</head>
<body>
</body>
<script src="main.js" type="text/javascript"></script>
</html>
Now I am getting this error:
"Uncaught ReferenceError: __dirname is not defined", source:
file:///Users/Praney/projects/webkit/nw-sample-apps/leaderboards/main.js(2)
main.js
process.argv.splice(2, 0, 'program.json');
process.chdir(require('path').join(__dirname, 'programs', 'server'));
require('./programs/server/boot.js');
This isn't how demeteorizer is meant to be used.. exactly/kind of.
You would use the output bundle on your deployed server to run as your meteor app, not put it in an existing meteor app.
The package.json that you get from it is slightly different to the one that meteor-npm would use.
When you've finished your meteor app you would use demeteorizer to create an easy bundle that can run on your server. If you uploaded it and untarred it:
You would install the npm modules by cding into the bundle and running npm install
You can run the app as normal as described in the docs.
The whole purpose of demeteorizer is to nodify your app, you wouldn't need to this on the platform you made it since all the npm modules would already be working. The problem it solves is usually with cross-archs, e.g if you made your app on OS X and it uses binary npm modules and the server uses Ubuntu (not os x)
I suppose node-webkit could also do it, you would need to use the root directory of demeteorize for this (seperate from your app). You can see there's a package.json already in it, perhaps the root directory you set it to use is that of your meteor app and not the untarred output of the demeteorized app?

Installing a Web Server for Node.js

I'm trying to follow a book I purchased called "Pro Angular JS", and I am having trouble getting a web server pointed to the right port. So I go to the command line, run Node, and the first error I get is this, when trying to install connect:
npm should be run outside of the node repl, in your normal shell.
(Press Control-D to exit.)
Ok, fair enough. So I do as the command specifies, and I get it to install just as the user, and it ends up adding a folder called node_modules in my user root folder. Ok, seems like everything still makes sense.
Now, the book tells me to create a server.js file within the Node.js installation folder. There is no Node.js installation folder actually created on my user. I see the node_modules folder for sure. So I'm guessing the root directory of my user is where node.js was installed but maybe it's hidden or something? I believe when I used the Mac installer for Node, it said it was created at usr/local/bin. But I have no idea if that is my user on my computer, or even more root access to my computer.
Lastly, back to this server.js file...so I created it with a text editor, containing this code:
var connect = require('connect');
connect.createServer(
connect.static(".../angularjs")
).listen(5000);
And of course they want me to add this file to the directory where my Node is installed. Currently, it's sitting where my current user (user is kst001) root directory is. This is also where my node_module folder was created when I installed it using the npm install connect line in the shell. They also wanted me to create a folder called angularjs, where I would store my app, and said to place it in the root directory where node.js was installed. Once again, sitting in the root directory with everything else. Yet, when I try and fire up my test document in port 5000 (localhost:5000/test.html), I get a "could not find page" error.
Already tried using this link to solve my problem, which seems dead on for my issue, but it resolved nothing:
Node / connect issue Object function createServer has no method static
I'm using a Mac, by the way. Any ideas, guys? Any help would be much appreciated.
The reason why connect.static() does not work is that the latest major version of connect (3.x) no longer contains all of the middleware that connect was bundled with in 2.x.
The readme for connect has a list of middleware references that show you the name of the module on npm that gives you the old functionality for each middleware (e.g. static is now broken out into its own module serve-static).
I'm following the same book/example and the following works. I claim no credit, it is from another Stack Overflow answer about setting up a simple server plus the contents of a comment on the same answer (question 24346161 link to it from here: nodejs connect cannot find static)
Because I used it in exactly the same learning context (book I also purchased called "Pro Angular JS") and I have been around the houses for 3 hours trying to sort this out (yes a complete novice), I thought I would post it here.
firstly from your node installation directory
npm install serve-static
secondly, your node server.js code for a simple static server to serve your angularjs directory contents in a localhost:5000 browser window, on a Windows 7 machine should be (as at July 2015) ...
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("./angularjs"));
app.listen(5000);
I just stuck a simple index.html file in the angularjs directory to begin with containing
connection working
to test it and it worked a treat.

WebRTC PeerConnection client nodejs

I'm trying to get WebRTC PeerConnection client on ubuntu to work with nodejs.
First I tried wrtc (https://github.com/js-platform/node-webrtc). I manage to run the client, but I cannot seem to fetch the the data stream. I've noticed the the RTCPeerConnection/PeerConnection on this module have different functions/implementation than when I run inside chrome. So I went looking for another nodejs PeerConnection.
I tried:
https://github.com/Rantanen/node-peerconnection
The already compiled version throws an error: _ZN6webrtc17BitrateController23CreateBitrateControllerEv
I figured that to get it to work properly, I have to compile the peerconnection on my machine.
I've followed http://www.webrtc.org/reference/getting-started and compile webrtc in the directory: /home/myuser/.webrtc/.
I have /home/myuser/.webrtc/trunk/out/(Debug/Release).
I have peerconnection_client executable.
Next I cloned: https://github.com/santiago/node-peerconnection (another implementation, with a make file).
When I'm trying to run the makefile (make WEBRTC_ROOT_PATH=/home/myuser/.webrtc/trunk)
I get the error:
src/node_peer_connection_client.h:9:18: fatal error: node.h: No such file or directory
include <node.h>
I have /usr/include/nodejs (and /usr/include/node symlink). I've noticed that there's an src dir inside (/usr/include/node/src/node.h).
Other things I tried:
https://github.com/js-platform/node-webrtc
https://github.com/Raynos/peer-connection
Basically, I'm trying to get a PeerConnection client that has been compiled on my machine to work with nodejs.
Any help will be appreciated ;)
Thanks
This looks promising: https://github.com/vmolsa/webrtc-native
Usage Example:
https://github.com/vmolsa/webrtc-native/blob/master/examples/node2browser/index.js:
API:
WebRTC.RTCPeerConnection
WebRTC.RTCIceCandidate
WebRTC.RTCSessionDescription
WebRTC.RTCDataChannel
WebRTC.MediaStream
WebRTC.MediaStreamTrack
WebRTC.getUserMedia
WebRTC.getSources
node-webrtc suppose to be the good choice for the time being, the project is alive. If you have any problems you should post an issue there.

Resources