简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM