Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to definition in visual studio code does not work

I am trying to setup visual studio code for a nodejs project following https://code.visualstudio.com/docs/languages/javascript

I created a jsconfig.json file in my root folder with the contents

{
  "compilerOptions": {
    "target": "ES5", //tried ES6 as well
    "module": "commonjs"
  }
}

This file tells VS Code you are writing ES5 compliant code and the module system you want to use is the commonjs framework. With these options set, you can start to write code that references modules in other files. For example, in app.js we require the ./routes/index module, which exports an Express.Router class. If you bring up IntelliSense on routes, you can see the shape of the Router class.

Although it doesn't seem to work with vscode 0.9.1. I don't get intellisense on my own modules. Go to definition doesn't work either.

https://code.visualstudio.com/docs/runtimes/nodejs#_great-code-editing-experiences

Is there a way to get go to definition working?

like image 751
Barış Uşaklı Avatar asked Oct 18 '15 03:10

Barış Uşaklı


1 Answers

TLDR; if you set your variables/functions directly on the exports object it seems to work.


After trying different ways to write modules I found out it works if you write your module in a certain way. Here is an example that works.

exports.myFunction = function (a, b) {
    return a + b;
};

Now if you import this module from another file you will get intellisense and go to definition will work. However if you write your module like this or any other variation it will not work.

var myModule = {};
myModule.myFunction = function (a,b) {
    return a + b;
};
exports = myModule;

Filed an issue on the vscode repository.

https://github.com/Microsoft/vscode/issues/15004#issuecomment-258980327

I will update my answer if this changes.

like image 108
Barış Uşaklı Avatar answered Oct 22 '22 07:10

Barış Uşaklı