Nodemon doesn't restart - node.js

I have a nodemon running in a docker-container with a mounted volume on OSX. Nodemon is receiving the file-change but it doesn't restart the server.
Output:
Creating piclet_web_1...
Attaching to piclet_web_1
web_1 | 7 Sep 13:37:19 - [nodemon] v1.4.1
web_1 | 7 Sep 13:37:19 - [nodemon] to restart at any time, enter `rs`
web_1 | 7 Sep 13:37:19 - [nodemon] watching: *.*
web_1 | 7 Sep 13:37:19 - [nodemon] starting node ./index.js
web_1 | restify listening at http://[::]:80 //normal output of started node-app
//file-change, which gets detected
web_1 | 7 Sep 13:37:30 - [nodemon] restarting due to changes...
//no output of restarted server
Dockerfile:
FROM node:0.12.7-wheezy
EXPOSE 80
RUN npm install nodemon -g
ADD app /app
WORKDIR app
RUN npm install
docker-compose.yml
web:
build: .
ports:
- "80:80"
volumes:
- ./app/lib:/app/lib
links:
- db
command: "nodemon -L ./index.js"
db:
image: mongo:3.1.7

I solved the issue by choosing another base-image for the container: node:0.12.7 instead of node:0.12.7-wheezy

Related

Run NodeJS server from AWS EC2

EC2 is properly deployed with my NodeJS server but I can't figure out how to keep it running without an ssh connection to the linux machine and running npm start
For Example:
[ec2-user#ip-172-31-89-105 me_server]$ npm start
> me-server#1.0.0 start
> nodemon index.js
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Started at 8080
Database Connected
I can run this command from an ssh tunnel from my local machine but I don't want my local machine to be in control of the server connection, how can I tell EC2 to run npm start on its own and keep it running indefinitely?

Cannot send requests to my express API w/ redis after docker-compose

I've been working on a small project with Node.js/Express + redis, with basic frontend w/o any frameworks (full code can be seen here). I've been testing my API with Postman (because my frontend is currently not working), and it worked like a charm.
So, after implementing redis to the project, I've tried adding it to my docker-compose.yml file, here it is and some other files:
docker-compose.yml
version: '3.8'
services:
redis:
container_name: redis-db
image: redis
networks:
- webnet
backend:
container_name: api
build: ./backend
restart: always
ports:
- '8080:8080'
networks:
- webnet
depends_on:
- redis
frontend:
container_name: frontend
build: ./frontend
restart: always
ports:
- '5000:5000'
networks:
- webnet
depends_on:
- backend
networks:
webnet:
config.yml
server:
host: localhost
port: 8080
app:
# '-1' for turning expiry off; otherwise, should be at least '1' (minute)
timeToLive: -1
db:
type: redis # ['in-memory', 'redis']
redis:
# select 'redis' if using docker-compose
host: redis # ['localhost', 'redis']
port: 6379
redis.js
import Redis from 'redis';
import config from 'config';
import AppError from '../utils/appError.js';
import { StatusCodes } from 'http-status-codes';
import { promisify } from 'util';
export const redisClient = Redis.createClient({
host: config.get('db.redis.host'),
port: config.get('db.redis.port'),
});
redisClient.on('error', function (err) {
throw new AppError(err, StatusCodes.INTERNAL_SERVER_ERROR);
});
redisClient.on('connect', () => {
console.log('šŸ’ƒ connected redis successfully šŸ’ƒ');
});
But, unfortunately, after docker-compose up I'm not able to make Postman requests anymore. Redis connected without any problems, in the console everything seems to be fine, and I even can access my frontend pages, but I cannot make requests to my backend anymore.
My frontend is running on localhost:5000, backend supposed to be running on localhost:8080, and redis seems to be running on redis:6379.
I've tried my best searching for the answer, but there are hundreds of questions on how to connect redis to your express project with docker-compose, but no questions about troubles accessing backend after connecting redis.
EDIT_1: here is what postman says:
POST http://127.0.0.1:8080/api/v1/queue
Error: socket hang up
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.28.3
Accept: */*
Postman-Token: a61d8b9c-a2f5-4fe4-92e1-921a5680bb85
Host: 127.0.0.1:8080
Accept-Encoding: gzip, deflate, br
Here is docker ps output:
f7f6fbe12863 project1_frontend "docker-entrypoint.sā€¦" 3 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp frontend
9802680a1a3d project1_backend "docker-entrypoint.sā€¦" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp api
66fde12be888 redis "docker-entrypoint.sā€¦" 3 minutes ago Up 3 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-db
And here are my console logs after docker-compose up:
api |
api | > queue-resolution-api#0.1.0 start /app
api | > nodemon server.js
api |
api | [nodemon] 2.0.12
api | [nodemon] to restart at any time, enter `rs`
api | [nodemon] watching path(s): *.*
api | [nodemon] watching extensions: js
api | [nodemon] starting `node server.js`
api | NODE_ENV: development
api | Listening on port 8080...
api | šŸ’ƒ connected redis successfully šŸ’ƒ
redis-db | 1:C 16 Aug 2021 14:35:18.728 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-db | 1:C 16 Aug 2021 14:35:18.728 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis-db | 1:C 16 Aug 2021 14:35:18.728 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-db | 1:M 16 Aug 2021 14:35:18.728 * monotonic clock: POSIX clock_gettime
redis-db | 1:M 16 Aug 2021 14:35:18.729 * Running mode=standalone, port=6379.
redis-db | 1:M 16 Aug 2021 14:35:18.729 # Server initialized
redis-db | 1:M 16 Aug 2021 14:35:18.729 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-db | 1:M 16 Aug 2021 14:35:18.729 * Ready to accept connections
redis-db | 1:M 16 Aug 2021 14:35:20.587 * DB saved on disk
frontend |
frontend | > frontend#1.0.0 start /app
frontend | > nodemon server.js
frontend |
frontend | [nodemon] 2.0.12
frontend | [nodemon] to restart at any time, enter `rs`
frontend | [nodemon] watching path(s): *.*
frontend | [nodemon] watching extensions: js,mjs,json
frontend | [nodemon] starting `node server.js`
frontend | Listening on port 5000...
I've figured out the answer. In my server.js file I've been forcing the app to listen on host = localhost, and after deleting the host part everything started working.
app.listen(port, host, () => {
console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
console.log(`Listening on port ${port}...`);
});

Dockerized Node App unable to connect to Dockerized Postgres Database

I have tried many possible solutions to it, Not able to make any of them work.
Here it goes:
I built a nodejs container and a postgres docker container. Used docker compose to configure them both and used Dockerfile to build the nodejs/typescript application.
docker-compose.yml
version: "3"
volumes:
pg_vol:
networks:
app-network:
driver: bridge
services:
db:
container_name: db
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_DB=db22
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=q123
volumes:
- pg_vol:/var/lib/postgresql/data
networks:
- app-network
webapp:
container_name: webapp
build:
context: ./
dockerfile: Dockerfile
environment:
- DATABASE_URL=postgres://postgres:q123#db:5432/db22
ports:
- "5000:5000"
networks:
- app-network
depends_on:
- db
Dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY ./dist .
CMD [ "npm", "start" ]
When I do docker-compose up --build, it shows an error while connecting to the postgres db. DB starts alright.
Error STDOUT
Starting db ... done
Starting webapp ... done
Attaching to db, webapp
db |
db | PostgreSQL Database directory appears to contain a database; Skipping initialization
db |
db | 2020-11-24 16:32:25.918 UTC [1] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db | 2020-11-24 16:32:25.918 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db | 2020-11-24 16:32:25.918 UTC [1] LOG: listening on IPv6 address "::", port 5432
db | 2020-11-24 16:32:25.923 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db | 2020-11-24 16:32:25.928 UTC [25] LOG: database system was shut down at 2020-11-24 16:32:18 UTC
db | 2020-11-24 16:32:25.933 UTC [1] LOG: database system is ready to accept connections
webapp |
webapp | > backend-postgres#1.0.0 start /usr/src/app
webapp | > node app.js
webapp |
webapp | undefined
webapp | undefined
webapp | running in-code config
webapp | Error: connect EHOSTUNREACH 172.19.0.2:5432
webapp | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
webapp | errno: -113,
webapp | code: 'EHOSTUNREACH',
webapp | syscall: 'connect',
webapp | address: '172.19.0.2',
webapp | port: 5432
webapp | }
webapp | npm ERR! code ELIFECYCLE
webapp | npm ERR! errno 1
webapp | npm ERR! backend-postgres#1.0.0 start: `node app.js`
webapp | npm ERR! Exit status 1
webapp | npm ERR!
webapp | npm ERR! Failed at the backend-postgres#1.0.0 start script.
webapp | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
webapp |
webapp | npm ERR! A complete log of this run can be found in:
webapp | npm ERR! /root/.npm/_logs/2020-11-24T16_32_26_955Z-debug.log
webapp exited with code 1
Please help with correcting me.
System:
Fedora 33 (Workstation Edition)
EDIT:
I restarted the docker container with command docker start webapp. Same error as above.
Judging by logs: the app exits with 1 because the host at port 5432 was unreachable,maybe the database was not ready yet (you can verify it by checking if db22 is created on db container ).What you can is : add restart policy on webapp ,using this :
webap : ..
restart_policy: condition: on-failure
then check then if the problem persists
My system is Fedora 33 (Workstation Edition). This had some issues with Docker.
Turns out:
With the release of Fedora 33, Docker is not supported by Fedora 33 officially. Oct 29, 2020
I uninstalled docker. Rebooted my laptop.
Then install Podman and Podman Compose. Everything works perfectly fine.
Thank you all for help.

App crashed in nodemon

I am trying to run in debug mode using Git Bash a nodejs application.
I did it several times but on the recent ones it doesn't work anymore.
When I run the command:
npm run startwindows:inspect
it says:
14 Apr 15:10:59 - [nodemon] v1.4.1
14 Apr 15:10:59 - [nodemon] to restart at any time, enter `rs`
14 Apr 15:10:59 - [nodemon] ignoring: C:\XXX\.git/**/* C:\XXX\node_modules/**/* C:\XXX\bower_components/**/* .sass-cache
14 Apr 15:10:59 - [nodemon] watching: C:\XXX\server/**/* C:\XXX\config-local/**/*
14 Apr 15:10:59 - [nodemon] watching extensions: js,json,yaml
14 Apr 15:10:59 - [nodemon] starting `node --inspect --debug-brk energyreports.js`
14 Apr 15:10:59 - [nodemon] child pid: 6352
Unable to open devtools socket: address already in use
14 Apr 15:10:59 - [nodemon] app crashed - waiting for file changes before starting...
14 Apr 15:11:03 - [nodemon] watching 37,149 files
14 Apr 15:11:03 - [nodemon] watching 37,149 files - this might cause high cpu usage. To reduce use "--watch".
What can I provide more is that startwindows:inspect is a script from the package.json file and it looks like this:
"startwindows:inspect":"set NODE_ENV=dus&& set NODE_CONFIG_DIR=./config-local/&& nodemon -V -w server -w config-local -e js,json,yaml --inspect --debug-brk| bunyan -o short"
What means by using --watch? Is this a normal case when testing it in debug? My CPU % utilization looks fine all the time.
Well it says exactly what the problem is.
Unable to open devtools socket: address already in use
Previous instance of the app is still running. Just restart your computer or kill related running processes.
New Edit: By OwlyMoly
Killing processes at a particular port: (In Mac)
If you know the port where your app running at, then check for that port ID and kill the process.
To check port id:
lsof -i :YourPort
or
lsof -n -iTCP:YourPort | grep LISTEN
Then get PID from the result and use the below command to kill that process.
To Kill process using PID:
kill -9 PID

How to fix Nodemon exit on changes issue

everyone. I like to use nodemon while developing with node.js because its restart on changes feature. I never had any problem at all. But since I updated Ubuntu from 13.04 to 13.10 it fails to restart on every change I make, so I have to restart my server manually. I run my apps as always, like so:
nodemon app.js
and with some config options like:
nodemon app.js production
The problem is, like I said, that even if the file I'm modifying is a client side one, I happen to get:
9 Nov 13:48:29 - [nodemon] v0.7.10
9 Nov 13:48:29 - [nodemon] to restart at any time, enter `rs`
9 Nov 13:48:29 - [nodemon] watching: /home/alevardi/render/wip/paraiso
9 Nov 13:48:29 - [nodemon] starting `node app.js`
Express server listening on port 3000
Successfully connected to MongoDB
9 Nov 13:48:51 - [nodemon] exiting
The last line of the output above, appears when I change a file. So, what can i do for fixing this awful issue? It is driving me crazy!
NOTE: I've tried with Forever and supervisor with the same results.
Thanks a lot for your help.

Resources