Typescript auto-import local linked modules

Hi,

 

I've setup a project using lerna which links local folders as node_modules when necessary.

Let's say I have a this structure :

|- packages/

|-- a/

|-- b/

B is a typescript library with an barrel index at its root folder.

B is linked in A as a scoped module in the scope @test (so its local path is a/node_modules/@test/b).

Each time I'm trying to import a file in A, from B obviously, its auto imported path is relative to the file importing it (ex. ../../b/src/[...]), instead of putting the alias @test/b.

 

Any idea on how to achieve this result please :) ?

 

Edit : I've tried to use paths in the tsconfig.json without success so far.

9 comments

>B is linked in A as a scoped module in the scope @test

not sure I follow you. Is a file in your project dir a symlink to a file in node_modules? Or?

 

I'd say that Webstorm has multiple issues with soft links, due to its own limitations and limitations of Typescript service used for Typescript parsing, etc. see https://youtrack.jetbrains.com/issue/WEB-25420 and linked tickets

0

>>B is linked in A as a scoped module in the scope @test

>not sure I follow you. Is a file in your project dir a symlink to a file in node_modules? Or?

B is installed in the folder A node_modules, each package A and B are node projects to say so.

Would you prefer a github project as a sample ?

0

strange - to me, Webstorm never uses relative paths to import files from node_modules.

Yes, github project would be helpful

0

It apparently does if you specify a bad path in its tsconfig.json and / or if you specify an include rule.

I've managed to make it work here : https://github.com/nimaen/lerna-webstorm-import/blob/master/packages/test/index.ts

And here is the tsconfig content important part without any include rule :

```

"baseUrl": ".",

"paths": {

   "@ab/core": [

      "../core/*"

   ]

}

```

Tanks for the support :)

 

0

With your project, WebStorm does respect "paths": specified in tsconfig.json and generated imports as

import {CoreConfig, RootConfig} from "@ab/core";

0

Yes indeed.

It looks like however that Webstorm puts some relative paths if you use an "include" rule in the tsconfig.

0

Please can you provide a sample? What changes do I need to make to the provided project to recreate the issue?

0

Oh nervermind my last response, looks like it works with a correct include path like here :

https://github.com/nimaen/lerna-webstorm-import/blob/master/packages/test/tsconfig.json

Thanks Elena, I think you can close the ticket now.

0

Please sign in to leave a comment.