Angular 6 workspaces - Relative imports to an application from a library fails.

Using angular 6 new workspaces, you're able to place multiple projects alongside each other. When importing files from a project to another project, Intellij defaults to importing it by relative path, which will fail. Instead if should import it using the project-name.

My example: I have an application named 'my-app' and a library called 'my-lib. 'my-lib' contains a component called 'my-component' that I want to use in 'my-app'. When importing 'my-component' Intellij imports it like this...

import {MyComponent} from "../../../my-lib/src/lib/my-component";

This will not work, it needs to be

import {MyComponent} from "my-lib";

Is it possible for Intellij to default to this instead?

8 comments

You need modifying path mappings in tsconfig.json accordingly.

By default, ng generate library myLib creates the following mappings in main tsconfig.json:

"paths": {
"myLib": [
"dist/my-lib"
],
"myLib/*": [
"dist/my-lib/*"
]
}

But the dist folder is not there, and specified paths can't be resolved. You have to change the config as follows:

 

"paths": {
"myLib": [
"dist/my-lib"
],
"myLib/*": [
"dist/my-lib/*",
"projects/my-lib/src/lib/*"
]
}
0

Thank you for your quick reply.

I always build my library (using ng build --prod --project my-lib) after making changes, so the dist/common is indeed there.

Trying your suggestion makes the imports look like this:

import {MyComponent} from "my-lib/my-component";
0

dist is auto-excluded due to https://youtrack.jetbrains.com/issue/WEB-30918 for better performance... And auto-import doesn't consider excluded folders:(

1

Does this mean there is no way forward right now? Are there any changes that could be made in future versions?

0

You can mark it as "not excluded": from the folder right-click menu, choose Mark directory as/Not Excluded

0

Thank you, it worked. Not sure if this is the best approach, but maybe it's the best available one for now!

1

The bug in this post is fixed. Why is this still an issue?

0

>The bug in this post is fixed

sorry, what bug?

0

Please sign in to leave a comment.