iceblueorbitz
iceblueorbitz

Reputation: 1060

Function exports in Node.js show up as properties in VSCode

This isn't technically wrong, so I didn't file as a bug on GitHub, but it seems like there should be better behavior (or perhaps a better way I can define exports).

Say I have:

function one() {
    // do stuff
}

function two() {
    one()
}

module.exports = {one, two};

In another file when I require this module, both one and two show up as object properties (blue icons). VSCode doesn't seem to recognize that they are in fact functions. This is the same behavior if you define named anonymous functions such as module.exports = {one: function () {}}; which makes sense when written that way. But if I simply want a function to show up as a function in exports, the only way I can seem to make it work is:

module.exports = {
   one() { /* do stuff */ },
   two() { /* do more stuff */},
};

This works in some cases, but if two needs to call one and both functions need exported, you must use this. to access the other members.

How can I define a bunch of functions in a module that can utilize each other, and also export them all so that they show up as functions (purple icon) in other modules in VSCode?

EDIT

The issue here is getting VSCode to recognize and hint functions as such when requiring them from other modules, instead of them showing up as 'properties' of the module.

Upvotes: 0

Views: 1266

Answers (1)

Karim Chaari
Karim Chaari

Reputation: 402

This approach can allow you to export your functions and call your local functions :

let self = {
  one: function() {
    return 'one';
  },
  two: function() {
    return self.one();
  }
};
module.exports = self;

On the other hand, if your blue icon still not displaying, you could name your functions:

let self = {
  one: function SayOne() {
    return 'one';
  },
  two: function CallOne() {
    return self.one();
  }
};
module.exports = self;

Finally, you can import your functions this way :

const { one, two } = require('./your_file');
console.log(two());

Upvotes: 1

Related Questions