resolve the error module.js:339 - node.js

This is my server.js code and I want a solution on how to resolve the error: can't find the module 'connect'
var connect = require('connect');
var port = 3000;
connect.createServer connect.static(__dirname )
).listen(port);
console.log('Connect via port '+port);

You need to install the package connect. Use the npm install command to install the connect package.
npm install connect

Related

How to fix 'Error: Cannot find module 'supertest''

I am new to nodejs. I got one nodejs application and i was just trying to run the tests this app contains. So i tried to run npm test command after installing all packages using npm install. But npm test is throwing below error always
internal/modules/cjs/loader.js:657
throw err;
^
Error: Cannot find module 'supertest'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:655:15)
at Function.Module._load (internal/modules/cjs/loader.js:580:25)
at Module.require (internal/modules/cjs/loader.js:711:19)
at require (internal/modules/cjs/helpers.js:14:16)
I tried to uninstall and install supertest but it did not help. I see there is no node_modules folder inside supertest. Would that be an issue? How do we fix it?
Here is the file which is using supertest
var fs = require('fs')
var request = require('supertest')
var config = require('./config').getConfig()
var url = config.url
var caCert
if (config.caCertFile) {
caCert = fs.readFileSync(config.caCertFile)
}
var preparedRequest = function () {
return caCert ? request.agent(url, { ca: caCert }) : request(url)
}
module.exports = preparedRequest
It seems that supertest is not in your package.json depencendies. So If you run npm install, it will not install supertest.
Just run npm install supertest to install supertest, or npm install --save supertest to install it and add the dependency in your package.json so the next time you run npm install it will install supertest too :)

Application failed to start (port 8080) not available

I am trying to deploy my node.js application (with express and mongoose) to openshift and I am not able to do so. The application works perfectly on my local environment.
my entry point is the file /bin/www
I establish this as the entry point on openshift with this line in the package.json file (per this thread):
"main": "bin/www",
I have made sure to set my mongodb connection using environment variables according to the guide like so:
// default to a localhost configuration:
var mongoConnectionString = 'mongodb://127.0.0.1/code-blog';
// if OPENSHIFT env variables are present, use the available connection info:
if (process.env.OPENSHIFT_MONGODB_DB_PASSWORD) {
mongoConnectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "#" +
process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}
mongoose.connect(mongoConnectionString);
The error that I get is:
remote: Waiting for application port (8080) become available ...
remote: Application 'codeblog' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 558a25bd5973ca7a74000162 (Error activating gear: CLIENT_ERROR: Failed to
execute: 'control start' for /var/lib/openshift/558a25bd5973ca7a74000162/nodejs
remote: #<IO:0x00000000b49380>
remote: #<IO:0x00000000b49308>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed
To ssh://558a25bd5973ca7a74000162#codeblog-donaldsubert.rhcloud.com/~/git/codebl
og.git/
29635a8..7a0e926 master -> master
This is peculior to me because I do not specify port 8080 anywhere. In fact, the default port is specified here:
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
app.set('port', port);
var server = http.createServer(app);
I am not really sure where to go from here. I don't seem to have enough information to determine my next step.
[edit]
I added some logging to test what port this is running on, but the logging statement is never run. Here is the code
console.log("TEST TEST TEST");
var app = require('../app');
var debug = require('debug')('ProjectTemplate:server');
var http = require('http');
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
console.log("PORT: ", port);
and output
TEST TEST TEST
module.js:340
throw err;
^
Error: Cannot find module './routes/logIn'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/lib/openshift/558a25bd5973ca7a74000162/app-root/ runtime/repo/app.js:26:13)
TEST TEST TEST is from a logging statement at the beginning of the entry point file. Something seems to fail before it hits the console.log("PORT: ", port); It is probable that this is something to do with app.js where the MongoDb connection is made.
[/edit]
Had exactly same error message: Application 'appname' failed to start (port 8080 not available) on open shift node app
After a lot of reading found out that many different users came to different solutions for the same error message, including myself. So I'd advice not to look for quick solutions for this error.
The most important step is step 1 in the below list.
My solution was to add a missing dependency in package.json, for my particular case I needed to add "bcrypt":"~0.8.5", such a stupid thing!
Now, how did I get to fix the issue only knowing the "port 8080 not available" error:
ssh'd into the app, went to the app repo dir (cd $OPENSHIFT_REPO_DIR) and run npm start
Got [...] Error: Cannot find module 'bcrypt' [...]
Logged out from ssh, run npm info bcrypt | grep "version:", it returned "0.8.5"
Added entry "bcrypt":"~0.8.5" to my package.json and commited/pushed changes.
Problem solved, app runs!
The error turned out to be a generic error that had nothing to do with the port. Apparently, if there is any fatal javascript error, it gives this message.
I have mentioned in detail here ... Application 'appname' failed to start (port 8080 not available) on open shift node app
The solution is you need to specify IP address when listening server.
This is how I have fixed it.
var express = require('express');
var app = express();
var http = require('http');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
server();
});
I don't think that this line will create the server on the specified port
var server = http.createServer(app);
You will need to tell it to listen on the port like so:
server.listen(port);
While your app is listening on the correct port, the thing missing is that you don't specify the IP address to listen on. Add something like the following below app.set('port', port);
app.set('ipaddr', server_ip_address);
For me it was the connection string. The one that was suggested right after you created the mongodb cartridge is not working: Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/'nodejs'
But from this article, by using the OPENSHIFT_MONGODB_DB_URL variable it worked.
if(process.env.OPENSHIFT_MONGODB_DB_URL){
mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + db_name;
}

nodejs is throwing symbol lookup error

This is the first time I am getting this error using nodejs, not sure what is it and how to come out of it. Below is the error:
node: symbol lookup error: /home/zishan/node_modules/zmq/build/Release/zmq.node: undefined symbol: zmq_sendmsg
when I am getting above error:
I have two test scripts testServer.js and testClient.js, when I am running testServer.js it's working fine but when I am running testClient.js by node testClient.js than testServer.js node window immediately throws above error.
below is the code for both scripts.
// testServer.js
var zmq = require('zmq');
var socket = zmq.socket('push');
socket.bindSync('tcp://127.0.0.1:3000');
console.log('Server is open on port 3000');
setInterval(function(){
console.log('sending work');
socket.send('some work');
}, 500);
Below is testClient.js
var zmq = require('zmq')
var socket = zmq.socket('pull');
socket.connect('tcp://127.0.0.1:3000');
console.log('client connected to port 3000');
socket.on('message', function(msg){
console.log(msg.toString());
});
My Env.
Ubuntu - 13.04
Nodejs - 0.10.24
Recently i reinstalled node using How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) because i got hit by this error node-gyp rebuild.
Any help is much appreciated.
Can you try uninstalling latest zmq module by npm uninstall zmq and try with specific version of zmq npm install zmq#2.4.0

Redis in Nodejs on Cloud9 IDE: [Error: Auth error: undefined]

Here is my code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server),
redis = require("redis"),
env = {PORT: process.env.PORT || 8080, IP: process.env.IP || "localhost"};
client = redis.createClient(env.PORT , env.IP);
client.on("error", function(err) {
console.log(err);
});
server.listen(env.PORT);
console.log("Server started # " + env.IP + ":" + env.PORT);
After trying to run, I received the followings on the console:
Running Node Process
Your code is running at 'http://modified.address.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
info: socket.io started
Server started # modified.ip.address.1:8080
[Error: Auth error: undefined]
I tried establishing the connection, and it connects to the IP and PORT perfectly. However, the error [Error: Auth error: undefined] appears and stops there. I Googled the error, the supports from the IDE I used..., and surprisingly, there are only 7 links to my problems. So I think it may be a hole in my knowledge or it is not really a problem yet a thing I don't know to work it out. All I could pull out from those Google results were (I was not sure) I need to use client.auth(pass) right after creating it. But where should I find the password? When I installed it npm install redis I didn't configure anything and wasn't told to set password whatsoever. So I reach the impasse.
I use Cloud9 IDE (c9.io), and the modules used as shown in the code above.
----With best regards,
----Tim.
I've found out what was wrong.
I did install Redis, but that is a Redis library that acts like a bridge between Redis driver and NodeJS. On Cloud9, I have to manually install Redis, too.
So it would take 2 commands to actually install Redis:
Install the Redis Driver on Cloud9
nada-nix install redis
Install Redis library for NodeJS
npm install redis
Thanks for anyone who was trying to help me.
You can run the redis-server using your own config file.You can create your own config like below.
//port and ip of ur redis server
port 6371
bind 127.0.0.1
//password for this server
requirepass ucanmentionurpwd
//storing snapshots of the data
save 60 1
dbfilename dump.rdb
dir /tmp/db
//starting redis server
redis-server //ur config file location
See this link for redis configuration
https://raw.github.com/antirez/redis/2.6/redis.conf
If you mention requirepass with your password means only you need to do
client.auth('urPwd');
Otherwise no need to call the client.auth method.

Cannot find module 'connect' on Mac OS X Lion

I installed connect module using NPM running the following command:
npm install connect
it created the module in /Download/usr/node_modules/connect folder. I created a file which
uses connect module using
var connect = require('connect');
var util = require('util');
function sendjson(res,obj)
{
res.writeHead(200,{'Content-Type':'application/json',});
var objstr = JSON.stringify(obj);
util.debug('SENDJSON' + objstr);
res.end(objstr);
}
var server = connect.createServer(
connect.router(function(app){
app.get('/foo', function(req,res){
sendjson(res,{path:'foo'});
})
app.get('/bar', function(req,res){
sendjson(res,{path:'bar'});
})
})
);
server.listen(3000);
I run node createServer.js and it throws in the terminal and it gives me the following error.
Cannot find module 'connect'
NPM modules by default need to be installed locally in the folder that contains the source file that uses them. So if your source file is in /Desktop/nodescripts, you should run "npm install connect" in that same folder. That will create node_modules folder in that path, and your script will be able to find it.

Resources