zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
275 stars 34 forks source link

Error: Cannot find module 'some/path/Class' #196

Closed derian-all-win-software closed 1 year ago

derian-all-win-software commented 1 year ago

Version

I have used two versions of the libraries, the first one is 2.6.11 and the other one is 3.0.3 being the newer release, in both the error occurs.

Error

Error: Cannot find module '../../../contexts/shared/infrastructure/WinstonLogger'
Require stack:
- /Users/user/app/node_modules/node-dependency-injection/dist/lib/Loader/FileLoader.js
- /Users/user/app/node_modules/node-dependency-injection/dist/lib/Loader/JsFileLoader.js
- /Users/user/app/node_modules/node-dependency-injection/dist/lib/index.js
- /Users/user/app/src/apps/backoffice/di/index.ts
- /Users/user/app/src/apps/backoffice/BackendApp.ts
- /Users/user/app/src/apps/backoffice/start.ts

Description

When I use ContainerBuilder with the YamlFileLoader class as follows:

import { ContainerBuilder, YamlFileLoader } from 'node-dependency-injection';

const container = new ContainerBuilder();
const loader = new YamlFileLoader(container);
loader.load(`${__dirname}/application.yaml`);

export default container;

when trying to resolve the paths from the Yaml file it is not able to resolve them, I use a file called application.yaml where I simply do the necessary imports from my other *.yaml files that have the paths to the necessary classes, it would be something like this:

# application.yaml

imports:
  - { resource: ./shared/application.yaml }
  - { resource: ./users/application.yaml }
# shared/application.yaml

services:
  Shared.Logger:
    class: ../../../contexts/shared/infrastructure/WinstonLogger
    arguments: []

I have made sure that the file path is correct but I still can't resolve the classes, I tried to register the classes using the register method and that way it works using the same file path.

import WinstonLogger from '../../../contexts/shared/infrastructure/WinstonLogger';
...
container.register('Logger', WinstonLogger);
zazoomauro commented 1 year ago

Hello @derian-all-win-software

can you please show me the content of the file WinstonLogger

Remember, every single service/dependency needs to be exported as default or use https://github.com/zazoomauro/node-dependency-injection/wiki/NamedMultiple

derian-all-win-software commented 1 year ago

Hi @zazoomauro, this is the content of the WistonLogger class:

import winston, { Logger as WinstonLoggerType } from 'winston';
import Logger from '../domain/Logger';

enum Levels {
  DEBUG = 'debug',
  ERROR = 'error',
  INFO = 'info'
}

class WinstonLogger implements Logger {
  private logger: WinstonLoggerType;

  constructor() {
    this.logger = winston.createLogger({
      format: winston.format.combine(
        winston.format.prettyPrint(),
        winston.format.errors({ stack: true }),
        winston.format.splat(),
        winston.format.colorize(),
        winston.format.simple()
      ),
      transports: [
        new winston.transports.Console(),
        new winston.transports.File({ filename: `logs/${Levels.DEBUG}.log`, level: Levels.DEBUG }),
        new winston.transports.File({ filename: `logs/${Levels.ERROR}.log`, level: Levels.ERROR }),
        new winston.transports.File({ filename: `logs/${Levels.INFO}.log`, level: Levels.INFO })
      ]
    });
  }

  debug(message: string) {
    this.logger.debug(message);
  }

  error(message: string | Error) {
    this.logger.error(message);
  }

  info(message: string) {
    this.logger.info(message);
  }
}
export default WinstonLogger;

The Logger interface would look like this:

export default interface Logger {
  debug(message: string): void;
  error(message: string | Error): void;
  info(message: string): void;
}
zazoomauro commented 1 year ago

It looks good...

so the issue seems to be related with the real path. can you please try to follow this in order to avoid ......... hell https://github.com/zazoomauro/node-dependency-injection/wiki/CustomRelativeServiceDirectory

derian-all-win-software commented 1 year ago

Hey @zazoomauro, this has solved the problem for me, thank you very much for the help.