1

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

4
  • 3
    Your module has an export assignment and other exported elements. Commented Sep 30, 2017 at 3:55
  • @DaveNewton i am not able to understand what you are trying to say here, can you help me with some links or explain to me in detail
    – Bikash
    Commented Sep 30, 2017 at 3:59
  • 2
    You have an exports =. You have `exports.foo =‘. The first is an export assignment. The rest are other exported elements. Commented Sep 30, 2017 at 4:10
  • @DaveNewton What should be changed ? As stated above i want to maintain a single file for all ops, or shall i leave it like that bcoz my code runs properly
    – Bikash
    Commented Sep 30, 2017 at 4:21

1 Answer 1

1

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);
};
1
  • Thanks for explaining the issue, but i am actually sending the redisClient from a different file! I also wonder is it working than !
    – Bikash
    Commented Sep 30, 2017 at 5:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.