Command not working in the middle of a Heroku tutorial [duplicate] - node.js

This question already has answers here:
Port in use when launching Node.js app with Heroku Foreman
(2 answers)
Error: That Port is already in use (Heroku/Django)
(1 answer)
Closed 21 days ago.
I am following a tutorial in Heroku documentation (https://devcenter.heroku.com/articles/getting-started-with-nodejs) and hit the problem hereafter, when running the command:
% heroku local web
On a Mac (M1), though all the first part of the tutorial went fine, in the section Run the app locally, this is what I get (not what is supposed to happen):
myveryname#Me-MacBook-Air node-js-getting-started % heroku local web
[OKAY] Loaded ENV .env File as KEY=VALUE Format
2:20:52 PM web.1 | > node-js-getting-started#0.3.0 start
2:20:52 PM web.1 | > node index.js
2:20:52 PM web.1 | node:events:491
2:20:52 PM web.1 | throw er; // Unhandled 'error' event
2:20:52 PM web.1 | ^
2:20:52 PM web.1 | Error: listen EADDRINUSE: address already in use :::5000
2:20:52 PM web.1 | at Server.setupListenHandle [as _listen2] (node:net:1432:16)
2:20:52 PM web.1 | at listenInCluster (node:net:1480:12)
2:20:52 PM web.1 | at Server.listen (node:net:1568:7)
2:20:52 PM web.1 | at Function.listen (/Users/myveryname/Documents/Heroku/node-js-getting-started/node_modules/express/lib/application.js:635:24)
2:20:52 PM web.1 | at Object.<anonymous> (/Users/myveryname/Documents/Heroku/node-js-getting-started/index.js:10:4)
2:20:52 PM web.1 | at Module._compile (node:internal/modules/cjs/loader:1126:14)
2:20:52 PM web.1 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
2:20:52 PM web.1 | at Module.load (node:internal/modules/cjs/loader:1004:32)
2:20:52 PM web.1 | at Function.Module._load (node:internal/modules/cjs/loader:839:12)
2:20:52 PM web.1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
2:20:52 PM web.1 | Emitted 'error' event on Server instance at:
2:20:52 PM web.1 | at emitErrorNT (node:net:1459:8)
2:20:52 PM web.1 | at processTicksAndRejections (node:internal/process/task_queues:83:21) {
2:20:52 PM web.1 | code: 'EADDRINUSE',
2:20:52 PM web.1 | errno: -48,
2:20:52 PM web.1 | syscall: 'listen',
2:20:52 PM web.1 | address: '::',
2:20:52 PM web.1 | port: 5000
2:20:52 PM web.1 | }
[DONE] Killing all processes with signal SIGINT
2:20:52 PM web.1 Exited with exit code null
myveryname#Me-MacBook-Air node-js-getting-started %
Searching the net seems to suggest that Mac may make use of the 5000 port on its own.
When I look at http://localhost:5000/ in my web browser, it looks like the port 5000 is indeed somewhat busy (though the page shows completely white).
But I have no idea what it is doing. And even if I restart the computer that does not change.
I must not be the only one to have hit this issue. So what is the correct way to handle this situation and be able to continue with the end of the tutorial?

On mac Air Play Control Center listens on port 5000.
You can disable airplay receiver to release the port or you can change the port on your code to something like 5001
you can also run this command lsof -i :5000 to check which process is using 5000 port
Reference:https://developer.apple.com/forums/thread/682332

Related

nodejs container won't connect to redis container docker-compose

I have an express API running in a node container and a redis container.
When attempting to connect the node container to redis I am greeted with the following error.
api_1 | events.js:291
api_1 | throw er; // Unhandled 'error' event
api_1 | ^
api_1 |
api_1 | Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
api_1 | Emitted 'error' event on RedisAdapter instance at:
api_1 | at RedisClient.onError (/usr/src/api/node_modules/socket.io-redis/dist/index.js:65:22)
api_1 | at RedisClient.emit (events.js:314:20)
api_1 | at RedisClient.on_error (/usr/src/api/node_modules/redis/index.js:342:14)
api_1 | at Socket.<anonymous> (/usr/src/api/node_modules/redis/index.js:223:14)
api_1 | at Socket.emit (events.js:314:20)
api_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
api_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
api_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
api_1 | errno: 'ECONNREFUSED',
api_1 | code: 'ECONNREFUSED',
api_1 | syscall: 'connect',
api_1 | address: '127.0.0.1',
api_1 | port: 6379
api_1 | }
Here the code that is establishing the connection:
const redisClient = require("redis").createClient({host: process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost'});
I have tried changing 'localhost' to "redis" to "redis://redis:6379". Nothing works.
As I understand it "localhost" in the context of a container refers to its own internal network, however both nodejs and redis are running on the same network which I've specified in my docker compose file.
Docker compose snippet
api:
build: ./api
ports:
- "3004:3004"
volumes:
- type: bind
source: ./api
target: /usr/src/api
working_dir: /usr/src/api
depends_on:
- redis
networks:
- backend
redis:
image: "redis:latest"
ports:
- "6379:6379"
hostname: redis
networks:
- backend
```
What could this be. All the "solutions" that I've managed to find through research don't work for me.
Just to close the question, when trying to connect to localhost inside the container it assumes the container itself and because the node.js container doesnt publish on the redis port the connection is refused. On the other hand because using docker-compose, you get a default docker network binding them together so using the container\service names as internal secured DNS records works.
for a more detailed explenation of the docker networking inner works see this thread

Getting an error while running "nodemon app" comannd

i've downloaded express package and nodemon, I created a server and rendered all of my ejs files . while running "nodemon app" command I'm getting this error, what specifically the problem is? :
PS C:\Users\user\Desktop\my port> nodemon app
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1316:16)
at listenInCluster (net.js:1364:12)
at Server.listen (net.js:1450:7)
at Function.listen (C:\Users\user\Desktop\my port\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\user\Desktop\my port\app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1343:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
Your system has something already running on port 3000.
you can do either of 2,
Kill process running on 3000
Or Use different port in your current app (like 3001, 5000, etc)
hi use this and kill the process directly;
netstat -ano | findstr :XXXX
XXXX <<<<----put the number of the port you have problem, system show it always, then when you press enter the system show you a message with a real port that one is what you need killing, so put this:
taskkill /F /PID XXXX
XXXX <<<<----the number of the port you want to kill and what system gave you after the first command
type this in command prompt
netstat -ano | findstr :XXXX <-THIS IS THE PORT NUMBER YOU used
And it will give you something like this
TCP 0.0.0.0:8080 || 0.0.0.0:0 || LISTENING | tttt
TCP [::]:8080 || [::]:0 || LISTENING || tttt<--thiss is the number you needed
then type this in command prompt
taskkill /PID tttt /F <--then enter
result will be something like -->
The process with PID tttt has been terminated.

How to fix Listen EADDRINUSE in ubuntu

How can I resolve this issue?
1|server | at listenInCluster (net.js:1327:12)
1|server | at Server.listen (net.js:1414:7)
1|server | at Object.<anonymous> (/root/signalmaster/server.js:41:8)
1|server | at Module._compile (internal/modules/cjs/loader.js:776:30)
1|server | at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
1|server | at Module.load (internal/modules/cjs/loader.js:653:32)
1|server | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
1|server | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
1|server | at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
1|server | code: 'EADDRINUSE',
1|server | errno: 'EADDRINUSE',
1|server | syscall: 'listen',
1|server | address: '::',
1|server | port: 8080 }
My server and turnserver status is Online but our clients can't communicate or see each other.
UPDATE:
see this pm2 list
try in the console :
killall node
and the problem should be fixed
The following IP address already used. In same IP address you can run the server by different ports. The port must be opened.
* If it's live server and already running httpd .
<VirtualHost *:80>
ServerName api.xxxx.com
#DocumentRoot /var/www/test
CustomLog /var/www/test/access.log common
ErrorLog /var/www/test/error.log
UseCanonicalName Off
UserDir disabled
<Location "/">
ProxyPreserveHost On
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
</Location>
</VirtualHost>

Port specified when running node is undefined

The port is specified when i ran the node project says undefined.
> sf-chain#1.0.0 dev E:\System\dev\node\sf-chain
> nodemon ./app "HTTP_PORT=3002" "P2P_PORT=5002" "PEERS=ws://localhost:5001"
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./app HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001`
process.env.HTTP_PORT :: undefined
HTTP_PORT :: 3001
Listening for peer-to-peer connections on: 5001
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3001
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (E:\System\dev\node\sf-chain\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\System\dev\node\sf-chain\app\index.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
[nodemon] app crashed - waiting for file changes before starting...
I ran the first instance using
npm run dev
const HTTP_PORT = process.env.HTTP_PORT || 3001;
The application starts successfully running on port 3001 since i didn't specify HTTP_PORT at the run command.
But when trying to run another instance by specifying HTTP_PORT using the below command
npm run dev HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001
I get this error.
Error: listen EADDRINUSE :::3001
Which means the HTTP_PORT specified at run time is seen as undefined that's why it's trying to using the 3001 port for the first instance.
Error: listen EADDRINUSE
- error comes when somthing is already running/listen on that port
Its because sometime instance of same nodejs acquire port and didnt kill/exit the process properly,
if you have Ubuntu check first which process is using port by
lsof -i :3001 // Port no.
then output will be some thing like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2496 nasiruddin 13u IPv6 37696 0t0 TCP *:3001 (LISTEN)
kill using PID
kill 2496
then start again

Failing to connect to MongoDB on 27017 in NodeJs

I have updated NodeJs to V 0.12.7 and now when i try to start express server it gets started but its not getting connect to mongodb. Here's the stack trace.
F:\Node Restful API with Express4\server
> nodemon
12 Sep 10:50:51 - [nodemon] v1.3.7
12 Sep 10:50:51 - [nodemon] to restart at any time, enter `rs`
12 Sep 10:50:51 - [nodemon] watching: *.*
12 Sep 10:50:51 - [nodemon] starting `node server.js`
Magic happens on port 4040
events.js:85
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:78)
at emit (events.js:118:17)
at null.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:19)
at emit (events.js:110:17)
at Socket.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:14)
at Socket.emit (events.js:107:17)
at net.js:950:16
at process._tickCallback (node.js:355:11)
12 Sep 10:50:52 - [nodemon] app crashed - waiting for file changes before starting...
On Windows the localhost will not be available if the computer is not connected to the internet. Hence we need to write mongodb://127.0.0.1:27017 to connect to MongoDB.

Resources