Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigService Injection Issue in Nx/NestJS Library #29136

Open
1 of 4 tasks
ziyaDev opened this issue Dec 1, 2024 · 3 comments
Open
1 of 4 tasks

ConfigService Injection Issue in Nx/NestJS Library #29136

ziyaDev opened this issue Dec 1, 2024 · 3 comments
Assignees

Comments

@ziyaDev
Copy link

ziyaDev commented Dec 1, 2024

Current Behavior

I created a new Nx-based NestJS library and used the ConfigService from @nestjs/config. The ConfigModule is imported in the library, and the ConfigService is injected into one of the services. However, when I use this service in a consuming NestJS app, I encounter the following error:

[Nest] 21784  - 12/01/2024, 7:33:51 AM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'get')

TypeError: Cannot read properties of undefined (reading 'get')
    at EnvService.getServerEnv (/Users/ziyadev/Desktop/repos/fannaq/dist/apps/testing/webpack:/packages/env/src/nestjs/env.service.ts:12:38)

This indicates that ConfigService is not properly injected, even though ConfigModule has been imported both in the library and as a global module in the app.

Expected Behavior

The ConfigService should be correctly injected into the library service, and configuration values should be accessible without errors.

GitHub Repo

No response

Steps to Reproduce

Nx Report

Node           : 20.15.1
OS             : darwin-x64
Native Target  : x86_64-macos

nx (global)        : 20.0.0
nx                 : 20.0.0
@nx/nest           : 20.0.0
@nx/workspace      : 20.0.0
@nestjs/config     : 2.3.1
typescript         : 5.5.4

Failure Logs

> nx run testing:build:development

asset main.js 11.6 KiB [emitted] (name: main) 1 related asset
asset assets/.gitkeep 0 bytes [emitted] [from: apps/testing/src/assets/.gitkeep] [copied]
orphan modules 2.77 KiB [orphan] 4 modules
runtime modules 670 bytes 3 modules
built modules 8.16 KiB [built]
  modules by path ./ 8.04 KiB
    modules by path ./apps/testing/src/app/*.ts 4.56 KiB
      ./apps/testing/src/app/app.module.ts 1.34 KiB [built] [code generated]
      ./apps/testing/src/app/app.controller.ts 1.6 KiB [built] [code generated]
      ./apps/testing/src/app/app.service.ts 1.62 KiB [built] [code generated]
    ./apps/testing/src/main.ts 665 bytes [built] [code generated]
    ./packages/env/src/nestjs/index.ts + 4 modules 2.83 KiB [not cacheable] [built] [code generated]
  modules by path external "@nestjs/ 126 bytes
    external "@nestjs/common" 42 bytes [built] [code generated]
    external "@nestjs/core" 42 bytes [built] [code generated]
    external "@nestjs/config" 42 bytes [built] [code generated]
webpack 5.96.1 compiled successfully in 7317 ms

———————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target build for project testing (13s)

Debugger listening on ws://localhost:9229/d9d08e05-e76b-4972-98e9-200ab780f2ab

Debugger listening on ws://localhost:9229/d9d08e05-e76b-4972-98e9-200ab780f2ab
For help, see: https://nodejs.org/en/docs/inspector

[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [NestFactory] Starting Nest application...
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [InstanceLoader] EnvModule dependencies initialized +25ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +3ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [RoutesResolver] AppController {/api}: +11ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [RouterExplorer] Mapped {/api, GET} route +4ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG [NestApplication] Nest application successfully started +3ms
[Nest] 21784  - 12/01/2024, 7:33:41 AM     LOG 🚀 Application is running on: http://localhost:3000/api
[Nest] 21784  - 12/01/2024, 7:33:50 AM     LOG [EnvService] undefined
[Nest] 21784  - 12/01/2024, 7:33:51 AM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'get')

TypeError: Cannot read properties of undefined (reading 'get')
    at EnvService.getServerEnv (/Users/ziyadev/Desktop/repos/fannaq/dist/apps/testing/webpack:/packages/env/src/nestjs/env.service.ts:12:38)
    at AppService.getData (/Users/ziyadev/Desktop/repos/fannaq/dist/apps/testing/webpack:/apps/testing/src/app/app.service.ts:12:32)
    at AppController.getData (/Users/ziyadev/Desktop/repos/fannaq/dist/apps/testing/webpack:/apps/testing/src/app/app.controller.ts:11:28)
    at /Users/ziyadev/Desktop/repos/fannaq/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at InterceptorsConsumer.intercept (/Users/ziyadev/Desktop/repos/fannaq/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:12:20)
    at /Users/ziyadev/Desktop/repos/fannaq/node_modules/@nestjs/core/router/router-execution-context.js:46:60
    at /Users/ziyadev/Desktop/repos/fannaq/node_modules/@nestjs/core/router/router-proxy.js:9:23
    at Layer.handle [as handle_request] (/Users/ziyadev/Desktop/repos/fannaq/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ziyadev/Desktop/repos/fannaq/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/ziyadev/Desktop/repos/fannaq/node_modules/express/lib/router/route.js:119:3)

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@ziyaDev
Copy link
Author

ziyaDev commented Dec 1, 2024

The issue seems to be related to the SWC compiler. In the webpack.config.js, changing the compiler option from swc to tsc resolves the problem:

...
NxAppWebpackPlugin({
      target: 'node',
      compiler: 'tsc', // Switched from 'swc' to 'tsc' and the issue is gone
     ...
 
    }),

@ndcunningham
Copy link
Contributor

Hi, thanks for reporting this issue.

Can you provide a small repro highlighting the issue?

NestJS has a specific requirement for monorepos, it could be that we missed something for NxAppWebpackPlugin

@ndcunningham ndcunningham self-assigned this Dec 3, 2024
@ziyaDev
Copy link
Author

ziyaDev commented Dec 5, 2024

Here’s a small repro along with the NxAppWebpackPlugin configuration I’m using:

const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('node:path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/testing'),
  },
  plugins: [
    new NxAppWebpackPlugin({
      target: 'node',
      compiler: 'tsc',
      main: './src/main.ts',
      tsConfig: './tsconfig.app.json',
      assets: ['./src/assets'],
      optimization: false,
      outputHashing: 'none',
      generatePackageJson: false,
      progress: false,
    }),
  ],
};

project.json:

"targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "outputPath": "dist/apps/testing",
        "webpackConfig": "apps/testing/webpack.config.cjs",
        "main": "apps/testing/src/main.ts",
        "tsConfig": "apps/testing/tsconfig.app.json",
        "assets": ["apps/testing/src/assets"],
        "inspect": true
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "dependsOn": [
        "build",
        {
          "dependencies": true,
          "target": "build"
        }
      ],
      "options": {
        "buildTarget": "testing:build",
        "runBuildTargetDependencies": true
      },
      "configurations": {
        "development": {
          "buildTarget": "testing:build:development"
        },
        "production": {
          "buildTarget": "testing:build:production"
        }
      }
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants