14

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?

7
  • On a side note: did you consider using Typescript?
    – zlumer
    Commented Oct 20, 2015 at 8:53
  • It is an open source project written in javascript github.com/NodeBB/NodeBB Commented Oct 20, 2015 at 18:47
  • 1
    I can't get go to definition to work either, i'm trying with c# files/project Commented Nov 25, 2015 at 10:05
  • 1
    I'm using 0.10.6, go to definition seems to work only for stuff defined with var or function. Doesn't work for global things, doesn't work for anything declared with this.. Same for Intellisense I think.
    – Zonko
    Commented Jan 24, 2016 at 0:12
  • 1
    Did you have installed the typings referring to the API you are using?
    – NValchev
    Commented Jun 12, 2016 at 8:20

2 Answers 2

4

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.

6
  • 1
    I am having the same problem almost three and a half years later. Would you know if there have been any developments in this regard? Commented Jun 15, 2020 at 6:21
  • I think it was somewhat improved in the later versions but still not 100%, alot of similar issues are linked in this thread github.com/microsoft/TypeScript/issues/10868 Commented Jun 15, 2020 at 17:04
  • Any chances this has been fixed?
    – Vipresh
    Commented Nov 8, 2020 at 8:57
  • foo = function(){...} has no definition in VSC but function foo(){} does
    – bortunac
    Commented Nov 23, 2020 at 14:35
  • Yes, this is painful. I use the workaround: double click to select the word, and search by Cmd+Shift+F, and manually go to the file with the definition
    – Kaya Toast
    Commented Jan 7, 2021 at 12:42
0

If none of the solutions work give this one a try for mac users.

Sometime VSC caches the settings and how much you try it will behave the same way so try resetting VSC.

rm -rfv "$HOME/.vscode"
rm -rfv "$HOME/Library/Application Support/Code"
rm -rfv "$HOME/Library/Caches/com.microsoft.VSCode"
rm -rfv "$HOME/Library/Saved Application State/com.microsoft.VSCode.savedState"

Restart VSC - code . (Should start from scratch)

Also remove .vscode/ rm -rf .vscode/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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