3

I wrote this code in Typescript

import redis from 'redis';
import Promise from 'bluebird';

const DEFAULT_REDIS_TTL = 7200; // 2 hours

export default class Redis {

    readonly client : any;
    ttl : number = DEFAULT_REDIS_TTL;

    constructor(uri? : string, ttl : number = DEFAULT_REDIS_TTL) {
        this.client = redis.createClient(uri);
    }

    ...
}

export { Redis };

the compiler gives me this

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var redis_1 = require("redis");
var bluebird_1 = require("bluebird");
var DEFAULT_REDIS_TTL = 7200; // 2 hours
var Redis = (function () {
    function Redis(uri, ttl) {
        if (ttl === void 0) { ttl = DEFAULT_REDIS_TTL; }
        this.ttl = DEFAULT_REDIS_TTL;
        this.client = redis_1.default.createClient(uri);
        this.client.on('error', function (error) {
            console.error(error);
        });
...
exports.Redis = Redis;
exports.default = Redis

I don't know why 'redis.createClient(uri);just becomeredis_1.default.createClient(uri);`

I get the following error when trying to run my code in node

build/Lib/Cache/Redis.js:11
        this.client = redis_1.default.createClient(uri);
                                     ^

TypeError: Cannot read property 'createClient' of undefined

my tsconfig looks like this

{
    "compilerOptions": {
        "module": "mymodule",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
         "module": "commonjs",
        "outDir": "build"
    },

    "include": [
        "src/**/*.ts"
    ],

    "exclude": [
        "node_modules"
    ]
}

I run the compiler in main directory

tsc

I'm using node 6.7.2

4
  • Please note how export default class Redis in your code becomes a exports.default = Redis in output. The same is happening with redis, its' being imported as exports.default = redis, this is why it is being used redis_1.default.createClient. redis_1 is reference to exports, and redis_1.default is ref to exports.default. The problem you might experience is just a missing module (have you run npm i redis?). Try console.log(redis) in your code Commented Apr 19, 2017 at 2:27
  • Using different import syntax could help: import * as redis from 'redis'; and import * as Promise from 'bluebird'; Commented Apr 19, 2017 at 2:27
  • I run console.log(redis) and console.log(redis.createClient) and I got the redis object and output like [Function] for the second consolelog Commented Apr 19, 2017 at 2:38
  • i will try changing import syntax Commented Apr 19, 2017 at 2:39

1 Answer 1

2

Change your import to:

import * as redis from 'redis';

I don't think the typings for redis has a default export. Make sure you have the latest typings. If you have the latest typings, import redis from 'redis'; should throw a compile time error.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 as link suggests the same. import { createClient } from 'redis'; might also be an option, if you just want to use one method
thank you this worked ... it depends on the modules, i hadnt issue with mongodb-driver and express

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.