Typescript

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

19 September 2026 · 8 min read

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

Developing TypeScript applications with Lerna and Visual Studio Code offers numerous benefits, but developers often encounter a frustrating issue: Auto import in Visual Studio Code only offering absolute path when working with Lerna subpackages. This means that instead of VS Code suggesting relative imports like import { Component } from ‘../../components/Component’, it provides absolute paths such as import { Component } from ‘my-monorepo/packages/components/src/Component’. This can clutter your code, make refactoring difficult, and increase bundle sizes by preventing tree shaking. This article explores the root causes of this problem, provides practical solutions, and offers best practices to ensure cleaner and more maintainable code in your Lerna monorepo.

Understanding the Auto Import Issue in Lerna and TypeScript

The problem of auto import in Visual Studio Code only offering absolute path within Lerna monorepos using TypeScript stems from how TypeScript and VS Code resolve module paths. TypeScript’s module resolution algorithm, combined with VS Code’s auto-import feature, can sometimes prioritize absolute paths over relative paths, especially when dealing with the complexities of a monorepo structure. Specifically, the baseUrl and paths configurations in your tsconfig.json files play a crucial role. When these configurations are not correctly set up, TypeScript may struggle to accurately resolve module paths within the Lerna packages, leading to the generation of absolute import statements. A misconfigured baseUrl can prevent TypeScript from correctly determining the relative location of modules, while incorrect paths mappings can lead it to believe that modules are only accessible through absolute paths.

Furthermore, the way Lerna manages dependencies and symlinks can also contribute to this issue. Lerna often uses symlinks to connect packages within the monorepo, allowing them to depend on each other without being physically copied. While this approach is efficient, it can sometimes confuse TypeScript’s module resolution process. As a result, VS Code’s auto-import feature might default to suggesting absolute paths, as it perceives them as the more reliable way to access modules in other packages. Understanding these underlying mechanisms is the first step towards effectively addressing the auto import in Visual Studio Code only offering absolute path problem.

To summarize, the issue primarily arises from a combination of TypeScript’s module resolution behavior, VS Code’s auto-import functionality, and the specific configuration of tsconfig.json files within a Lerna monorepo. When these elements are not correctly aligned, developers are likely to experience the frustrating problem of absolute paths dominating their import suggestions. Addressing this requires a careful examination of these configurations and a deliberate effort to optimize them for the Lerna environment.

Configuring TypeScript for Relative Imports in Lerna

The key to resolving the auto import in Visual Studio Code only offering absolute path problem lies in properly configuring your TypeScript settings. Specifically, you’ll need to adjust the tsconfig.json files at both the root level and within each Lerna package. Start by ensuring that your root tsconfig.json file includes a compilerOptions section with appropriate settings for baseUrl and paths. The baseUrl should typically be set to “./”, indicating that the compiler should start resolving modules from the project root. The paths option allows you to define custom mappings for module paths, which can be particularly useful for aliasing packages within your monorepo.

Within each Lerna package, you should also have a tsconfig.json file that extends the root configuration. This allows you to inherit the base settings while customizing them for the specific needs of each package. Make sure that the include and exclude options are properly configured to include only the relevant source files and exclude any unnecessary files or directories. A common mistake is to have overly broad include patterns, which can confuse TypeScript’s module resolution process. You can find more details about configuring TypeScript module resolution on the official TypeScript website. TypeScript Module Resolution.

Here’s a featured snippet-optimized paragraph: To ensure relative imports, carefully configure the baseUrl and paths in your tsconfig.json files. The baseUrl should point to your project’s root, and the paths should map your package names to their respective source directories. For example, if you have a package named “components” located in “packages/components/src”, your paths configuration might look like this: “@my-monorepo/components/”: [“packages/components/src/”]. This tells TypeScript to resolve imports starting with “@my-monorepo/components” relative to the “packages/components/src” directory, promoting relative import suggestions.

VS Code Settings and Extensions for Auto Import

Beyond TypeScript configuration, VS Code settings and extensions can also influence the behavior of auto import in Visual Studio Code only offering absolute path. VS Code relies on TypeScript’s language service to provide auto-import suggestions, so ensuring that VS Code is using the correct TypeScript version is crucial. You can specify the TypeScript version that VS Code uses by opening the command palette (Ctrl+Shift+P or Cmd+Shift+P) and searching for “Select TypeScript Version.” Choose the version that corresponds to your project’s TypeScript dependency.

Additionally, certain VS Code extensions can enhance or interfere with the auto-import functionality. Extensions like “Path Intellisense” can provide more accurate and context-aware path suggestions, while others might introduce conflicts or unexpected behavior. Experiment with disabling or reconfiguring your extensions to see if they are contributing to the absolute path issue. You can also adjust VS Code’s settings related to auto-imports by opening the settings editor (Ctrl+, or Cmd+,) and searching for “auto import.” Here, you can configure options like javascript.suggest.paths and typescript.suggest.paths to control how VS Code suggests import paths. For more information on VS Code settings, refer to the official VS Code documentation. VS Code Settings.

Consider these key points regarding VS Code and auto-imports:

  • Ensure VS Code is using the correct TypeScript version for your project.
  • Experiment with disabling or reconfiguring VS Code extensions.
  • Adjust VS Code’s auto-import settings to control path suggestions.

Best Practices and Troubleshooting

Even with proper configuration, you might still encounter instances of auto import in Visual Studio Code only offering absolute path. In such cases, a few troubleshooting steps can help identify and resolve the problem. First, double-check your tsconfig.json files for any typos or inconsistencies. Ensure that the baseUrl and paths options are correctly defined and that the include and exclude patterns are appropriate. Use the TypeScript compiler to verify that your code compiles without errors. If you encounter compilation errors related to module resolution, it’s a clear indication that your tsconfig.json configuration needs adjustment.

Another useful technique is to manually type out a relative import statement and see if VS Code automatically corrects it to an absolute path. If it does, it suggests that VS Code is actively overriding your intended import behavior. In this case, review your VS Code settings and extensions for any potential conflicts. It’s also worth noting that the order in which you import modules can sometimes affect the auto-import suggestions. Try importing modules from within the same package before importing modules from other packages to see if it influences the behavior. Remember, consistent coding style and project structure significantly contribute to a smoother development experience. Maintainable codebase

Here are some recommended practices for managing imports in Lerna monorepos:

  • Use path aliases in your tsconfig.json to simplify import paths.
  • Maintain consistent coding style across all packages.
  • Regularly review and update your TypeScript configuration.

Consider this workflow for a new component:

  1. Create the component in the appropriate package.
  2. Add the component to the package’s public API (e.g., index.ts).
  3. Import the component using a relative path initially.
  4. Verify that VS Code does not automatically convert it to an absolute path.
Infographic here showing tsconfig.json configuration examples
FAQ: Auto Import in Visual Studio Code and Lerna ------------------------------------------------
Why does VS Code offer absolute paths for auto-imports in my Lerna monorepo?
This is often due to misconfigured tsconfig.json files, particularly the baseUrl and paths settings, or conflicts with VS Code extensions.
How do I force VS Code to suggest relative imports?
Correctly configuring your tsconfig.json files, using path aliases, and ensuring VS Code uses the correct TypeScript version can help prioritize relative imports.
What is the role of the baseUrl setting in tsconfig.json?
The baseUrl specifies the base directory for resolving non-absolute module names. Setting it to "./" typically helps with relative imports.
Can VS Code extensions interfere with auto-import behavior?
Yes, some extensions can conflict with or override VS Code's default auto-import functionality. Try disabling extensions to troubleshoot.
What is the best way to manage dependencies in a Lerna monorepo to avoid absolute imports?
Using Lerna's dependency management features and configuring path aliases in your tsconfig.json files can help manage dependencies and promote relative imports. Look at Lerna's documentation for more information. [Lerna Documentation](https://github.com/lerna/lerna).
Dealing with **auto import in Visual Studio Code only offering absolute path** in Lerna and TypeScript projects can be a hurdle, but with the right configuration and understanding, it's easily surmountable. By meticulously setting up your tsconfig.json files, optimizing your VS Code settings, and adhering to best practices for dependency management, you can ensure cleaner, more maintainable code. We've explored the common causes, outlined practical solutions, and provided troubleshooting tips to help you overcome this challenge.

Don’t let absolute paths clutter your codebase. Take the time to implement these strategies and enjoy the benefits of relative imports in your Lerna monorepo. Start by reviewing your tsconfig.json settings today. Consider sharing this article with fellow developers facing similar challenges, and explore other topics like optimizing Lerna for CI/CD or advanced TypeScript features for monorepos to further enhance your development workflow.

Question & Answer :
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.

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