Auto import in Visual Studio Code only offering absolute path with Lerna subpackages in TypeScript

TypescriptVisual Studio-CodeAuto Import

Typescript Problem Overview


For some reason, very recently my Visual Studio Code changed and started only offering absolute imports from the sub-package level with my Lerna packages, for example:

Enter image description here

As you can see, the auto import is suggesting the @package/server/src/database path to the file when it should just be ../database as the file being edited is within the same package and is just one folder below the file containing the database variable I'm trying to use.

Is this a bug or configuration issue?

I've set my Import Module Specifiersetting for TypeScript in Visual Studio Code to all three options (auto, relative, and absolute) and none of them seem to make any difference.

Typescript Solutions


Solution 1 - Typescript

In Visual Studio Code, menu FilePreferencesSettingsUser Settings,

"typescript.preferences.importModuleSpecifier": "relative"

It works fine for me. It imports

import { RegistrationComponent } from '../../abc-modules/registration/registration.component';

in place of

import { RegistrationComponent } from 'app/abc-modules/registration/registration.component';

Solution 2 - Typescript

In Visual Studio Code, menu File → Preferences → Settings → User Settings

search by importModuleSpecifier

enter image description here

Solution 3 - Typescript

I landed here from Google and had the opposite problem. My Visual Studio Code instance always imported the relative path even though it was from a different Lerna package.

It turns out that I simply forgot to add the package that it was wrongly importing to my consuming package’s package.json file.

Now, everything works as expected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnupheausView Question on Stackoverflow
Solution 1 - Typescriptlimbo93View Answer on Stackoverflow
Solution 2 - TypescriptLuiz Carlos Pedroso GomesView Answer on Stackoverflow
Solution 3 - TypescriptFlorian WendelbornView Answer on Stackoverflow