272

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.) Commented Sep 24, 2018 at 0:11
  • 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. /shrug Commented May 30, 2019 at 12:42

6 Answers 6

672

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';
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you! When it's set to auto, it seems to change between using relative and absolute.
John Tribe's advice is legit with Vanilla JS. However I use TypeScript with "src" folder for TS and "dist" folder for transplied JS, so absolute path causes runtime error.
JavaScript › Preferences: Import Module Specifier Preferred path style for auto imports. --> relative (Also, for jest testing, absolute paths break the test suite @JohnTribe.)
If it's not working: Note that you need to change the Typescript > Preference, not Javascript > Preference
There is also "project-relative" now, which makes it only prefer the relative import from within the same project. Useful for us with an NX monorepo setup, using tsconfig paths. Just using "relative" made vscode suggest relative imports instead of our configured paths.
|
109

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

search by importModuleSpecifier

enter image description here

5 Comments

It's works for me. Remember this tip works only typescript. You need the same steps to works do .js files
Oh it's doing shortest for default! I could not figure out why some imports were /app and some were ../ That's a good brainteaser. I failed.
+ better to use "typescript.preferences.importModuleSpecifier": "project-relative"
What If I wanted the import to be based on eslint rules? Ie. relative when current folder ie. "./XYZ" but absolute if "../"
Worked for me thanks!
25

My problem was that I had the baseUrl option set in my tsconfig.json file.

{
  "compilerOptions": {
    "baseUrl": ".", // remove
  },
}

After removing the option; VSCode immediately started importing via the relative path. The benefit of this method is that you can keep the VSCode option importModuleSpecifier set to shortest and relative path importing will still work.

3 Comments

This was the most acceptable solution in my case as I wanted to keep using my webpack aliases for imports, and other solutions did cause aliases to stop getting suggested.
Removing baseUrl might not be a good solution, because you might want to keep it for other imports. Unless you want to disable all absolute imports.
This one worked for me the best way; I needed to adjust the other aliases paths to be not relative to the base url but to the tsconfig
8

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.

1 Comment

Similarly, using NX and tsconfig paths, we fixed that by using "project-relative" as the importModuleSpecifier. Using just relative changed the other project's import to relative as well which is of course not what we wanted. "project-relative" works great though.
8

You might need a combination of baseUrl and paths.

tsconfig.json
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "holograph/src/*": ["src/*"]
    },
  }
VSCode settings
"typescript.preferences.importModuleSpecifier": "non-relative" // or "shortest"

My folders are like this. When I am in apps/app, I can auto-import in VSCode from packages with from 'holograph/src/ui/...'. But when I am in packages/holograph ui components, for a reason I haven't yet discovered, it auto-imports from src/ui, which gives a build error. I had to manually import from the package path hologrpah/src/ui. Doing the above paths import re-map fixed VSCode auto-imports.

node_modules
apps (next.js applications)
 ├── app
 |   └── pages
 ├── operator
 ├── storybook
packages (shared components & libraries)
 ├── holograph
 |   └── src
 |       └── ui
 ├── tsconfig
 ├── eslint-config-holograph
...

PS: This worked too, but I am not sure how valid this is. Basically setting the baseUrl one folder up. 🤷‍♂️

  "compilerOptions": {
    "baseUrl": "../",
    },
  }

1 Comment

Finally!!! This VSCode settings It does not appear anywhere!
2

For anyone looking for only relative path within a project,
Add a line of option in settings.json

"typescript.preferences.importModuleSpecifier": "project-relative"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.