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?

Sennen Randika
  • 1,566
  • 3
  • 14
  • 34
  • 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. – Romel Pérez Jan 18 '22 at 03:43
  • some like this discussed in here https://stackoverflow.com/questions/74296342/nx-error-for-relative-imports-within-the-same-project – Amir Rezvani Jul 21 '23 at 08:40

1 Answers1

1

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