nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) šŸ„¦

Home Page:https://nestjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nest can't resolve dependencies of the ServeStaticModule

UTGuy opened this issue Ā· comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I have a pre-configured module I built ...

import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from "node:path";

@Module({
    imports: [
        ServeStaticModule.forRoot({
            rootPath: join(process.cwd(), '..', 'client', 'dist')
        })
    ],
    exports: [
        ServeStaticModule
    ]
})
export class ClientStaticModule { }

Tests all pass...

import { INestApplication } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { Test } from '@nestjs/testing';
import { ServeStaticModule } from './client.module';

describe('ClientModule', () => {
    let app: INestApplication;

    it('should be defined', async () => {
        const moduleRef = await Test.createTestingModule({
            imports: [ServeStaticModule.registerAsync()]
        }).compile();

        app = moduleRef.createNestApplication();
        await app.init();

        expect(moduleRef.get(HttpAdapterHost)).toBeInstanceOf(HttpAdapterHost);
    });

    afterAll(async () => {
        await app.close();
    })
});

But when importing this into my other project it throws an error.

Error

[Nest] 6704  - 11/17/2023, 8:49:58 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the ServeStaticModule (SERVE_STATIC_MODULE_OPTIONS, AbstractLoader, ?). Please make sure that the argument HttpAdapterHost at index [2] is available in the ServeStaticModule context.

Potential solutions:
- Is ServeStaticModule a valid NestJS module?
- If HttpAdapterHost is a provider, is it part of the current ServeStaticModule?
- If HttpAdapterHost is exported from a separate @Module, is that module imported within ServeStaticModule?
  @Module({
    imports: [ /* the Module containing HttpAdapterHost */ ]
  })

Error: Nest can't resolve dependencies of the ServeStaticModule (SERVE_STATIC_MODULE_OPTIONS, AbstractLoader, ?). Please make sure that the argument HttpAdapterHost at index [2] is available in the ServeStaticModule context.

Potential solutions:
- Is ServeStaticModule a valid NestJS module?
- If HttpAdapterHost is a provider, is it part of the current ServeStaticModule?
- If HttpAdapterHost is exported from a separate @Module, is that module imported within ServeStaticModule?
  @Module({
    imports: [ /* the Module containing HttpAdapterHost */ ]
  })

    at Injector.lookupComponentInParentModules (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:254:19)
    at Injector.resolveComponentInstance (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:207:33)
    at resolveParam (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:128:38)
    at async Promise.all (index 2)
    at Injector.resolveConstructorParams (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:143:27)
    at Injector.loadInstance (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:70:13)
    at Injector.loadProvider (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/injector.js:97:9)
    at /workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/instance-loader.js:56:13
    at async Promise.all (index 0)
    at InstanceLoader.createInstancesOfProviders (/workspace/.yarn/unplugged/@nestjs-core-virtual-38511f33b2/node_modules/@nestjs/core/injector/instance-loader.js:55:9)

I've seen this on StackOverflow too... and I've checked the @nest/core and @nest/common versions and they are all the same...
I even set my yarn "resolutions" to ensure the correct verion

"resolutions": {
    "@nestjs/common": "10.2.9",
    "@nestjs/core": "10.2.9",
    "@nestjs/platform-express": "10.2.9"
  }

Minimum reproduction code

I cant link to an enterprise applicaiton

Steps to reproduce

No response

Expected behavior

The import to work when run

Package version

10.2.9

NestJS version

10.2.1

Node.js version

20.9.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@micalevisk I need to be clearer it seems... the unit test above passes with no issue. It's when I try to package that module in NPM and use it in another project... thats when the error happens

@UTGuy Oh so that's probably due to having more than one @nestjs/core/common loaded, which is expected due to how js works. Make sure you're using peer deps instead of hard dependencies

Hard to tell more without a repro.

Make sure you're using peer deps instead of hard dependencies

I will give that a shot

@micalevisk This indeed fixed it! Thank you!

@UTGuy how did you fix this?

For the record, in case it might be helpful for others...
I had updated a project I had not touched for months and upgraded all Nest libraries to latest 10+.
I ended up having exactly this error, and no override or change worked until out of desperation I rm -rf the node modules folder and reinstalled. This was the solution to actually get rid of the older version of @nestjs/core (which was indeed the culprit)