Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for absolute paths to autocomplete in Visual Studio Code (VSCode)?

When writing a javascript app, it's possible to create an .env file in the root directory containing just e.g:

NODE_PATH=src/

Which sets up allowing for absolute paths e.g: in import statements in code.

e.g: I can be working on the file /src/actions/index.js and enter:

import { SAVE_COMMENT } from "actions/types";

..and the import works, but there is no auto-complete and I wonder: Is it possible to auto-complete after I type just:

import { SAVE_COMMENT } from "actions/

?

Relative-path lookup continues to work great. In fact, the relative path lookup is one of my favorite features of vs-code and one of the reasons I use it, so it would be very nice for it to work when absolute paths are configured, too.

like image 252
Nathan W Avatar asked Jul 19 '18 11:07

Nathan W


People also ask

Does VS Code have autocomplete?

Visual Studio Code IntelliSense is provided for JavaScript, TypeScript, JSON, HTML, CSS, SCSS, and Less out of the box. VS Code supports word based completions for any programming language but can also be configured to have richer IntelliSense by installing a language extension.

Does Visual Studio have autocomplete?

The suggestion list of Automatic completion appears as soon as you start typing a new identifier. The suggestion list of Basic completion appears when you press the default Visual Studio IntelliSense shortcut Ctrl+Space . If necessary, you can always return to the Visual Studio's native's IntelliSense.

How do I enable autocomplete in Visual Studio?

Go to Tools | Options | Text Editor | C/C++. This dialog displays a window that allows you to toggle Automatic brace completion. Automatic brace completion is the feature where, when you type { , a corresponding } is automatically typed for you.


1 Answers

VS Code does not support using NODE_PATH for intellisense. To achieve what you want, create a jsconfig.json file at the root of your project with the contents:

{
    "compilerOptions": {
        "target": "ES6",
        "baseUrl": "./src"
    },
    "exclude": [
        "node_modules",
        "**/node_modules/*"
    ]
}

The important setting is baseUrl. It tells VS Code to resolve non-relative paths relative to the ./src folder

After configuring jsconfig and baseUrl, you can also set "javascript.preferences.importModuleSpecifier": "non-relative" in VS Code to specify that VS Code should always try to use paths to use the baseUrl

like image 143
Matt Bierner Avatar answered Sep 20 '22 16:09

Matt Bierner