when I debugged the code Transport object still exists then when I called trasport.close(), then right after that I get the error mentioned above for the transport case
what do you mean with “transport object still exists”? Of course it exists if you hold it in a variable. Things don’t auto destroy in JavaScript, the transport is just an object. The fact that you call close() on it doesn’t mean that it magically disappears. So please don’t try to go that way. Your code is calling producer.getStats() after closing the producer. That’s all.
Instead of doing the clean-up before calling by hand to the close() method, do the calling ot the close event, this way it will be clean-up no matter how way the object gets clossed.