Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to definition for ES6 imports if index.js is omitted

In ES6 (like node), importing a module like this

import something from 'something';

Is the same as

import something from 'something/index';

However, vscode appears incapable of performing "Go To Definition" if importing modules using the first method. If I append index, "Go To Definition" works. Is there a way to modify jsconfig.json to have vscode check the index file by default?

like image 745
wesley Avatar asked Feb 13 '17 08:02

wesley


2 Answers

I asked this question on github and received this response from Matt Bierner (Microsoft).

Does your project have a jsconfig.json? Can you make sure it contains the setting:

{ "compilerOptions": { "module": "commonjs" } }

Adding the above configuration to my jsconfig.json seems to have fixed the issue even though I'm using ES6 import and export syntax.

However this shouldn't be an issue since,

In a jsconfig, the module option only changes how paths are resolved

You can read more about the module setting here

like image 113
Brandon Boone Avatar answered Oct 26 '22 21:10

Brandon Boone


There is another solution (source). Include the following in your jsconfig:

"compilerOptions": {
    "module": "es6",
    "moduleResolution": "node"
},

Changing "module": "es6" to "module": "commonjs" worked for me too, but I prefer to keep module set to es6.

like image 28
jfrej Avatar answered Oct 26 '22 21:10

jfrej