Nest: Reuse module in npm package

Created on 21 Mar 2019  路  8Comments  路  Source: nestjs/nest


[ ] Regression
[x] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Hi all,
I am using Nest for the past year and I love it, kudos for the team!

I have a question and maybe a bug... I am trying to reuse my code in separate repos...

I have a npm package with one module "CommonModule". In that module I have a controller "MetadataController" and a service "MetadataService".

Then I have my App that uses that module, but when I include the import CommonModule into the AppModule I have an error:

Error
Nest cannot export a component/module that is not a part of the currently processed module (CommonModule). Please verify whether each exported unit is available in this particular context.

common.module:

import { Module } from '@nestjs/common';
import { MetadataController } from './controllers/metadata/metadata.controller';
import { MetadataService } from './services/metadata/metadata.service';

@Module({
  imports: [],
  controllers: [MetadataController],
  providers: [MetadataService],
  exports: [MetadataService],
})
export class CommonModule {}

app.module:

import { CommonModule } from 'my-repo';

@Module({
  imports: [
    CommonModule,
    TypeOrmModule.forRoot(AppModule.getTypeORMConfig()),
    GraphQLModule.forRoot({
      typePaths: ['./src/**/*.graphql'],
      definitions: {
        path: join(
          process.cwd(),
          './src/modules/generated-models/graphql-models.ts',
        ),
        outputAs: 'interface',
      },
      debug: true,
      playground: true,
    }),
  ],
  controllers: [],
  providers: [],
})

Expected behavior

It should be possible to reuse the module.

What is the motivation / use case for changing the behavior?

Reuse modules in external repos.

Environment


Nest version: 5.7.4

For Tooling issues:
- Node version: 11.0.0 
- Platform:  Linux
question 馃檶

All 8 comments

It should be possible to reuse the module.

It is possible.

Please, provide a minimal repository which reproduces your issue 馃檪

Hi,

Thank you for the quick response,

Please have a look to this repo, I removed the dependencies of the project to have a minimal repo...

https://github.com/JMCorks/nestjs-issue-1798

If I am missing something let me know (and sorry for waste your time 馃憤 )

Thank you!

@JMCorks

I haven't tested it, but it might have to do with the project structure, and the way nest module detection works...

Your project structure is like this:

/src/index.ts
/src/common.module.ts
/src/controllers/metadata.controller
/src/services/metadata.services

Instead, you should think about:

/src/index.ts
/src/common/common.module.ts
/src/common/common.controller.ts
/src/common/common.service.ts

Notice how module and services/controller are bundled together, and are in their own folder? This is the way the nest CLI creates them, perhaps try looking at that boilerplate generation.

Try that, I didn't notice anything else.

You should make @nestjs/common and other @nestjs/{x} packages peer dependencies, so they can share metadata each other.

@joeyslack
I was thinking that the package corresponds only to one module and that is why i didn't create a level "common". But I changed the structure, makes sense, thank you :+1:

@kamilmysliwiec I have to add the peers dependencies only in the npm package right?

"peerDependencies": {
    "@nestjs/common": "^6.0.2",
    "@nestjs/core": "^6.0.2",
    "@nestjs/swagger": "^3.0.1"
}

I changed the project structure and I added the peerDependencies but the error persists.

Thank you.

Remove & reinstall all packages. Your npm packages should only define peerDependencies - they shouldn't have @nestjs/{x} defined inside dependencies so they can share packages among themselves.

@kamilmysliwiec Thank you is working!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings