Max
Max

Reputation: 27

Redis is in docker undefined

I'm building a GraphQL API and I want to rate-limit some resolvers. For that, I'm using this rate limiter: https://github.com/teamplanes/graphql-rate-limit

This is how I implemented it:

import { createRateLimitRule, RedisStore } from 'graphql-rate-limit';
import redis from 'redis'

const rateLimit = createRateLimitRule({ 
  identifyContext: (ctx) => ctx.req.ip, 
  formatError: () => `Hey there, you are doing way too much`,
  store: new RedisStore(redis.createClient(6379, 'redis'))
});

But when I run docker-compose up I get this error from my NodeJS app:
TypeError: Cannot read property 'createClient' of undefined

Both my PostgreSQL database and the NodeJS app are starting normally.

This is my docker-compose file:

version: "3.7"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 4000:4000
    depends_on: 
      - postgres
      - redis
    env_file:
      - .env

  postgres:
    image: postgres
    ports:
      - 5432:5432
    env_file:
      - .env
    volumes:
      - ./init-schema.sql:/docker-entrypoint-initdb.d/init.sql
      - postgres:/var/lib/postgresql/data

  redis:
    image: redis
    command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"]
    hostname: redis
    volumes:
      - redis_data:/data
    ports:
      - 6379:6379

volumes: 
  redis_data:
    name: redis_data
  postgres:
    name: project_db

And my Dockerfile for the NodeJS file:

FROM node:12 AS builder
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/

RUN npm install
RUN npx prisma generate

COPY . .

RUN npm run build

FROM node:12

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/api ./api

EXPOSE 4000
CMD ["npm", "run", "dev"]

Also, everything works fine when I remove Redis. So please help me.

Upvotes: 1

Views: 1578

Answers (1)

andersryanc
andersryanc

Reputation: 979

it is saying that the redis var is undefined. You might need to:

import * as redis from 'redis'

or

const redis = require('redis')

and also make sure you have the package installed:

npm install redis

Upvotes: 5

Related Questions