Port in use when launching Node.js app with Heroku Foreman - node.js

I am trying to deploy my node.js app to heroku but when I try to launch it locally using foreman I am getting Error: listen EADDRINUSE. I have run a netstat and grepped for the port nothing else is using it and the server starts without issue when running directly as a node http server.
The app I am trying to deploy is using mongo and redis I am not sure if these components will effect the server starting with Foreman. Does anyone have any suggestions on areas I could look at for potential bugs?
foreman start
01:37:18 web.1 | started with pid 1835
01:37:18 web.1 | /usr/local/foreman/lib/foreman/process.rb:66: warning: Insecure world writable dir /usr/local in PATH, mode 040777
01:37:19 web.1 | events.js:72
01:37:19 web.1 | throw er; // Unhandled 'error' event
01:37:19 web.1 | ^
01:37:19 web.1 | Error: listen EADDRINUSE
01:37:19 web.1 | at errnoException (net.js:863:11)
01:37:19 web.1 | at Server._listen2 (net.js:1008:14)
01:37:19 web.1 | at listen (net.js:1030:10)
01:37:19 web.1 | at Server.listen (net.js:1096:5)
01:37:19 web.1 | at Function.app.listen (/Users/craig/Documents/Sandboxes /xxx/node_modules/express/lib/application.js:535:24)
01:37:19 web.1 | at Object.<anonymous> (/Users/craig/Documents/Sandboxes/xxx/web.js:25:5)
01:37:19 web.1 | at Module._compile (module.js:456:26)
01:37:19 web.1 | at Object.Module._extensions..js (module.js:474:10)
01:37:19 web.1 | at Module.load (module.js:356:32)
01:37:19 web.1 | at Function.Module._load (module.js:312:12)
01:37:19 web.1 | exited with code 8
01:37:19 system | sending SIGTERM to all processes
SIGTERM received
Thanks.
--Additional information--
The procfile just has one entry:
web: node web.js
and I have set the listener up as follows:
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});

I just ran into this on OS X. It looks like foreman picks port 5000 by default, which seems to conflict with Bonjour/mDNS/UPNP services. (From what I've read. I haven't taken the time to pick out which it is.)
However, you can change the port that foreman uses in two ways: specify the port on the command line or create a .foreman file with the port number specified there.
Good luck and happy coding!

I had the same issue, Error: listen EADDRINUSE means that a Node server is already running.
Check that you are not running a Node server for the same project locally. If you are working on a GitHub synced project locally (on port 5000 for instance) which is tied to Heroku you cannot run a local Node server for that project as the port will be in use twice.
I was actually running a Node server on a project in a different Terminal window and didn't notice immediately.

Related

Download or set Heroku app config variables while running the app locally

I have an app which I have downloaded from GitHub and after pushing it to Heroku it works perfectly fine. however when I use the heroku local command to run it locally it throws an error regarding configuration variables missing.
How can I download these config files to my local folder (do I even need to)? Do I need to create an .env file manually in order to make it work?
Here is the app:
const assert = require('assert');
const Provider = require('oidc-provider');
assert(process.env.HEROKU_APP_NAME, 'process.env.HEROKU_APP_NAME missing');
assert(process.env.PORT, 'process.env.PORT missing');
assert(process.env.SECURE_KEY, 'process.env.SECURE_KEY missing, run `heroku addons:create securekey`');
assert.equal(process.env.SECURE_KEY.split(',').length, 2, 'process.env.SECURE_KEY format invalid');
// new Provider instance with no extra configuration, will run in default, just needs the issuer
// identifier, uses data from runtime-dyno-metadata heroku here
const oidc = new Provider(`https://${process.env.HEROKU_APP_NAME}.herokuapp.com`, {
clients: [
{
client_id: 'foo',
redirect_uris: ['https://jwt.io'], // using jwt.io as redirect_uri to show the ID Token contents
response_types: ['id_token'],
grant_types: ['implicit'],
token_endpoint_auth_method: 'none',
},
],
cookies: {
keys: process.env.SECURE_KEY.split(','),
},
});
// Heroku has a proxy in front that terminates ssl, you should trust the proxy.
oidc.proxy = true;
// listen on the heroku generated port
oidc.listen(process.env.PORT);
and here is the error I get when I run "heroku local":
3:44:57 AM web.1 | assert.js:383
3:44:57 AM web.1 | throw err;
3:44:57 AM web.1 | ^
3:44:57 AM web.1 | AssertionError [ERR_ASSERTION]: process.env.HEROKU_APP_NAME missing
3:44:57 AM web.1 | at Object.<anonymous> (C:\Users\hmffa\Desktop\my-provider\src\index.js:4:1)
3:44:57 AM web.1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
3:44:57 AM web.1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
3:44:57 AM web.1 | at Module.load (internal/modules/cjs/loader.js:928:32)
3:44:57 AM web.1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
3:44:57 AM web.1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
3:44:57 AM web.1 | at internal/main/run_main_module.js:17:47 {
3:44:57 AM web.1 | generatedMessage: false,
3:44:57 AM web.1 | code: 'ERR_ASSERTION',
3:44:57 AM web.1 | actual: undefined,
3:44:57 AM web.1 | expected: true,
3:44:57 AM web.1 | operator: '=='
3:44:57 AM web.1 | }
[DONE] Killing all processes with signal SIGINT
3:44:57 AM web.1 Exited with exit code null
These variables often vary between environments. Some things, like your database connection string and security keys, should be different locally and in production.
However, Heroku documents an easy way to copy a config var from Heroku to a local .env file:
Sometimes you may want to use the same config var in both local and Heroku environments. For each config var that you want to add to your .env file, use the following command:
heroku config:get CONFIG-VAR-NAME -s >> .env
Do not commit the .env file to source control. It should only be used for local configuration. Update your .gitignore file to exclude the .env file.
Note that .env files aren't magic. Node.js won't use the values in there out of the box.
If you use heroku local to run your app locally (as you appear to be) you should find that your .env file gets used. If you run it another way, you may need to do additional work, e.g. perhaps by adding dotenv.

I got this error when run socket.io in docker

Error:
events.js:167
node_app_1 | throw er; // Unhandled 'error' event<br>
node_app_1 | ^
node_app_1 | Error: listen EADDRNOTAVAIL 50.177.55.2:3000<br>
node_app_1 | at Server.setupListenHandle [as _listen2] (net.js:1269:19)<br>
node_app_1 | at listenInCluster (net.js:1334:12)<br>
node_app_1 | at GetAddrInfoReqWrap.doListen [as callback] (net.js:1460:7)<br>
node_app_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:62:10)<br>
node_app_1 | Emitted 'error' event at:<br>
node_app_1 | at emitErrorNT (net.js:1313:8)<br>
node_app_1 | at process._tickCallback (internal/process/next_tick.js:63:19)<br>
Update:
Change your socker-io server's IP to 127.0.0.1
Your app is trying to listen on port 3000 and I believe it is already being used.
If you have some other service running on this port, you will either have to change the port of your socket-io server or follow the following process to kill the process that is already listening on port 3000.
Type the following in your terminal,
lsof -i:3000
It will show you the process that is listening to that port. Then you can kill that process using its process ID (PID)
kill -9 <PID>

ECONNREFUSED error while running express code

I am not able to run the server ...... I am getting the error as ECONNREFUSED
How to resolve this Error !
When i tried to use different ports .... all are giving me the same error !
ubuntu#ip-MyIP:~/rainmelon/projects/FindMyBuffet$ node app.js
Express server listening on port 7005
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
at Handshake.Sequence (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
at Protocol.handshake (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/protocol/Protocol.js:42:50)
at Connection.connect (/home/ubuntu/rainmelon/projects/FindMyBuffet/node_modules/mysql/lib/Connection.js:73:18)
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
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 Function.Module.runMain (module.js:497:10)
The traceback says where the exception is coming from:
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)
--------------------
...
at Connection.connect (.../node_modules/mysql/lib/Connection.js:73:18)
--> ^^^^^
at Object.<anonymous> (/home/ubuntu/rainmelon/projects/FindMyBuffet/app.js:15:12)
--> ^^^^^^^^^
So your app can't connect to MySQL.
This is usually down to either incorrect hostname/portname in your MySQL driver configuration, the MySQL server not running, or that your MySQL server isn't configured to listen on TCP sockets. See here.
Your mysql process is down, which means it is not running. You need to restart your mysql process (changing ports wont help).
To solve this problem you need to restart it. You can do by doing any of the following:
You can start your wamp or xamp server which will automatically start the process.
Or you can open the commandline prompt and start it manually like so "c:\wamp\bin\mysql\mysql5.5.24\bin\mysqld.exe"
Note that using the second method will require knowing the exact location of your wamp folder like I have used mine up top.(in quotes)
You may do a netstat to find out the pid of process running on the port 7005 and then go for a forceful kill with the pid got.
like
netstat -plten |grep 7005
kill -9 16085
where 16085 is the pid obtained from prev command. And restart the express app.
reference
How to kill a process running on particular port in Linux?

Can't start foreman cos node.js complains about some obscure module it can't find

trying to get a project working on my debian vm but foreman refuses to start. the node.js error message is not very helpful cos it doesn't even tell me what module can't be found.
sissi#debian:/media/fancystuff$ foreman start
22:05:33 web.1 | started with pid 2949
22:05:33 web.1 |
22:05:33 web.1 | module.js:337
22:05:33 web.1 | throw new Error("Cannot find module '" + request + "'");
22:05:33 web.1 | ^
'2:05:33 web.1 | Error: Cannot find module '/media/fancystuff/web.js
22:05:33 web.1 | at Function._resolveFilename (module.js:337:11)
22:05:33 web.1 | at Function._load (module.js:279:25)
22:05:33 web.1 | at Array.0 (module.js:484:10)
22:05:33 web.1 | at EventEmitter._tickCallback (node.js:190:38)
22:05:33 web.1 | process terminated
22:05:33 system | sending SIGTERM to all processes
btw 1: the error message is the same no matter if web.js is there of if it's deleted....which just adds to my confusion.
btw 2: "foreman check" gives me "valid procfile detected (web)"
sigh.
From this line:
'2:05:33 web.1 | Error: Cannot find module '/media/fancystuff/web.js
it looks like there is a non-printing character at the end of the line in your Procfile where you call node web.js since the ' character that was put after module name string ended up at the beginning of the line.
Editing Procfile and making sure that that special character is gone, your problem will probably be solved. I could replicate this on my Debian dev. server by adding <32 characters at the end of the line after web.js before EOL.
Most probably the Procfile was coppied from different OS that append carriage return e.g \r\n that not compatible with foreman. Just remove the trailing spaces or character in the Procfile and rerun. Or the simplest is to remove the Procfile and create using same OS
Example Procfile:
web: node web.js
And start the apps:
ubuntu#yadayada:~/heroku/web$ foreman start

multiple node.js + always = Error: watch EMFILE

I'm running three node.js applications on one server, all using Foreman to start up the apps through always.js.
Under zero load, one of my apps consistently throws the error Error: watch EMFILE and restarts. That application still works though, despite constantly throwing that error... I've tried to find more information about this error, but there's not a whole lot out there ("too many files open" or "increase ulimit".)
My question is: why would this be happening on an idle web application - and why just one out of three? It's not doing anything... Is it an issue with always.js? (There are two other node apps running through always on this machine though...) Just looking for some info as to what is causing this error, if it's serious, and how it can be resolved.
Thanks!
FYI, here is a relevant excerpt from the console:
01:17:07 web.1 | app listening on http://0.0.0.0:5000
01:17:07 web.1 | NODE_ENV = development
01:17:07 web.1 | opened connection to database!
01:17:07 web.1 | [always] Error: watch EMFILE
01:17:07 web.1 | [always] Error: watch EMFILE
01:17:07 web.1 | at errnoException (fs.js:636:11)
01:17:07 web.1 | at FSWatcher.start (fs.js:663:11)
01:17:07 web.1 | at Object.watch (fs.js:691:11)
01:17:07 web.1 | at Object.oncomplete (/home/jesse/local/nodev0.6.14/lib/node_modules/always/lib/monitor.js:62:36)
01:17:07 web.1 | [always] Restarting app.js with Node
I don't have experience with always.js but got similar errors with other node utilities that use the fs.watch command, which in turn uses linux's inotify API.
You might want to try checking the settings for inotify then:
cat /proc/sys/fs/inotify/max_user_instances
If the file exists, setting it to a higher value might help:
echo 8704 > /proc/sys/fs/inotify/max_user_instances

Resources