Bikash
Bikash

Reputation: 1522

TypeScript 'An export assignment cannot be used in a module with other exported elements'

My Objective is to have a single redis.js file for all redis operation from all over my code just like below snippet

const Redis = require('./handler/redis.js');
Redis.set();
Redis.get() etc ..

from my app.js
require('./core/redis')(redisClient);

Above code runs without any error But i am getting typescript error as 'An export assignment cannot be used in a module with other exported elements'

module.exports = function(redisClient) {

redisClient.on('error',function(err) {
    console.log('Redis Error ' + err);
});

redisClient.on('connected',function() {
    console.log('Redis Connected');
});

module.exports.set = function(key, value, next) {
    redisClient.set(key,value,next);
};

module.exports.get = function(key, next) {
    redisClient.get(key,next);
};

module.exports.del = function(key, next) {
    redisClient.del(key,next);
};
};

Even with this warning how my code is running

EDITED CODE : //from my app.js require('./core/redis').init(redisClient);

module.exports.init = function(redisClient) {
   redisClient = redisClient;
}

redisClient.on('error',function(err) {
    console.log('Redis Error ' + err);
});

redisClient.on('connected',function() {
    console.log('Redis Connected');
});

module.exports.set = function(key, value, next) {
    redisClient.set(key,value,next);
};

module.exports.get = function(key, next) {
    redisClient.get(key,next);
};

module.exports.del = function(key, next) {
    redisClient.del(key,next);
};

Is this the correct way to do it

Upvotes: 1

Views: 2082

Answers (1)

Arun Redhu
Arun Redhu

Reputation: 1579

You have written module.exports = function(redisClient) { as assignment statement for exports from this module and after that that you overriding this statement with module.exports.set, module.exports.get and module.exports.del That's why it is giving error 'An export assignment cannot be used in a module with other exported elements'. I don't know from where you are getting the redisClient parameter to the function. If this is already available in this current module then you can simply write the code as

redisClient.on('error',function(err) {
    console.log('Redis Error ' + err);
});

redisClient.on('connected',function() {
    console.log('Redis Connected');
});

module.exports.set = function(key, value, next) {
    redisClient.set(key,value,next);
};

module.exports.get = function(key, next) {
    redisClient.get(key,next);
};

module.exports.del = function(key, next) {
    redisClient.del(key,next);
};

Upvotes: 1

Related Questions