How to import absolute paths in a @nrwl/nx monorepo?
Asked Answered
L

1

7

I'm working on a @nrwl/nx monorepo. I want to import the folders inside the project src by the absolute paths. I tried specifying the baseUrl but didn't work. The only solution worked is, adding the path to the monorepo root tsConfig.json file as follows.

"paths": {
   "*": ["apps/my-app/src/*"]
}

But, the problem is, if I have another project, I will have to add that project as well to this path. I tried something as follows.

"paths": {
   "*": ["apps/*/src/*"]
}

But, this doesn't work anymore. It doesn't match the project folder name.

How can I solve this? Or, is there any better way to import by absolute paths?

Lipkin answered 15/1, 2022 at 3:4 Comment(2)
Suggestion: This is a TypeScript configuration question. Maybe you can change the title and be more specific of the file structure of the project and it may attract more attention.Pedometer
some like this discussed in here #74296842Annora
H
2

I'm facing the same problem, due to organizing common DTOs and Event.ts files in the nx monorepo. I found useful to update the tsconfig.base.json with a simpler path shortcut, that allow cross app imports and at the same time mantains the options of setting an absolute path in the single apps tsconfig.json file.

Here's my base.json:

"baseUrl": ".",
"paths": {
  "libs": [
    "libs/"
  ],
  "app1: [
    "apps/app1/"
  ],
  "app2": [
    "apps/app2/"
  ],
}

Now I have a sort of absolute imports that point to app names as base:

import {CreateUserEvent} from 'libs/events/create-user.event';

This is a random file in the app1/src/app/ folder that import a file in libs folder

Folder structure is:

root ('.') 
|__ app1/src/app/file_with_import.ts
|__ ... 
|__ ...
|__ libs/events/create_user.event.ts

Hope it helps

Habiliment answered 16/6, 2022 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.