110
votes

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.

2
I'll play with this if you set up a repository that I can clone to reproduce the problem. (I don't want to spend the time to try to set up a project like yours by myself only to potentially fail to reproduce the problem.)Matt McCutchen
Fwiw, at least one user has importModuleSpecifier set to relative in workplace and user files and it still imports with a full pat -- edit: this question suggested TS version -- there, a different version and issue -- could cause weirdness. Changing from TS 2.3.2 to 3.4.5 resolved this issue for me. /shrugruffin

2 Answers

311
votes

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';
5
votes

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.