简体   繁体   中英

Nx Monorepo - How to Use NestJs Library inside the Main Nest App

I am working in an Nx monorepo workspace. The workspace structure is something like below:

在此处输入图像描述

The api is a NestJS app, and the data-access-scripts-execute is a NestJS lib.

I don't know how I should import and use the mentioned lib controllers inside my api app. The following is my code:

api 's AppModule:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';

// This line says 'Cannot find module '@something/data-access-scripts-execute'
// or its corresponding type declarations.'
import { SomeController } from '@something/data-access-scripts-execute';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true, expandVariables: true }),
    MongooseModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        uri: 'mongodb://localhost/something',
        useFindAndModify: false,
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

lib's Controller:

import { Controller, Post } from '@nestjs/common';
import { SomeService } from './some.service';
import { SomeEntity } from './schemas/some.schema';

@Controller('some-address')
export class SomeController {
  constructor(private readonly _service: SomeService) {}
  
  @Post()
  async create(): Promise<SomeEntity> {
    console.log('create called');
    
    return this._service.create();
  }
}

I should also mention that:

  • All of my Angular libs can see other Angular libs
  • My api app can see all of my Angular libs
  • All of my NestJS libs cannot see other libs
  • All of my NestJS libs cannot be seen by other libs and apps

By the word see I actually mean @something which my project name.

The following is also my tsconfig.base.json file which is located in the root of the workspace:

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": ["es2017", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@something/system": ["libs/system/src/index.ts"],
      "@something/feature-some-angular-lib": [
        "libs/feature-some-angular-lib/src/index.ts"
      ],
      "@something/feature-some-angular-lib": [
        "libs/feature-some-angular-lib/src/index.ts"
      ],
      "@something/shared": ["libs/shared/src/index.ts"],
      "@something/data-access-scripts-execute": ["libs/data-access-scripts-execute/src/index.ts"]
    }
  },
  "exclude": ["node_modules", "tmp"]
}

I should also say that the image is not complete becuase of privacy reasons.

I ran into a similar issue and the reason for that was "baseUrl": "." property in the app's tsconfig.app.json

Try to remove that and see if it resolves your issue. This is only my guess and I cannot say much without looking at your tsconfig.app.json

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