0

Wondering if anyone could help me out with this, as I'm new to NestJS.

I'm trying to use Redis in my existing NestJS service, rather than creating a separate microservice like Nest documents in their examples. When I import redis from node-redis it comes back as undefined.

token.service.ts

import { Injectable } from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';
import redis from 'redis';

@Injectable()
export class TokenService {
    constructor() {
        // create new redis client with default options
        this.client = redis.createClient();
        this.client.on('error', err => console.error(err));
    }

    ...
}

The error I'm getting: Cannot read property 'createClient' of undefined

I've never seen undefined imports in Node, so I'm wondering if this a NestJS specific issue, or if it has to do with the redis package I'm using.

Any help is appreciated.

1 Answer 1

4

You're likely having an issue with the import statement.

Try replacing

import redis from 'redis';

with:

import * as redis from 'redis';

or

const redis = require('redis');
Sign up to request clarification or add additional context in comments.

3 Comments

You are completely correct. Thank you. I don't know why but I didn't even think to look at the exports.
This is right. I find it strange that in regular TS based ExpressJS projects can import Redis with import redis from 'redis'; while NestJS requires us to do it import * as redis from 'redis';
@KrishnanSriram TS based projects use precompilers. They often do code transformations for compatibility reasons, which might be why it works in one project and not the other. In your tsconfig.json file, check to see if allowSyntheticDefaultImports is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.