12

I have a nestjs app and I need to bundle it with webpack. Bundle should containt the whole script with all files. So I created my webpack configuration:

const webpack = require('webpack');
const path = require('path');

module.exports = {
  entry: {
    backend: './src/index.ts',
  },
  target: 'node',
  module: {
    rules: [
      {
        test: /.ts$/,
        use: 'ts-loader',
      },
      {
        test: /\.(graphql|gql)$/,
        exclude: /node_modules/,
        loader: 'webpack-graphql-loader',
      },
      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: "javascript/auto",
      }
    ],
  },
  externals: ['fsevents'],
  mode: 'production',
  resolve: {
    extensions: ['.ts', '.mjs', '.js', '.json'],
  },
  output: {
    path: path.join(__dirname, 'bundled'),
    library: 'handler',
    libraryTarget: "commonjs2"
  },
  plugins: [
    new webpack.IgnorePlugin({
      checkResource(resource) {
        const lazyImports = [
            '@nestjs/microservices',
            'cache-manager',
            '@nestjs/microservices/microservices-module',
            '@nestjs/websockets/socket-module',
            'class-transformer',
            'apollo-server-fastify',
        ];
        if (!lazyImports.includes(resource)) {
          return false;
        }
        try {
          require.resolve(resource);
        } catch (err) {
          return true;
        }
        return false;
      },
    }),
  ],
};

It seems it works fine but when I change my code a little it started to throw a strange error.

Error: Nest can't resolve dependencies of the r (?). Please make sure that the argument r at index [0] is available in the s context.

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

So I tried to represent minimal code example and found when error occured. The problem in logger

import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class SecondService {
  private readonly logger = new Logger(SecondService.name); // this line throw an error
}

So, if you just comment logger declaration - everything will start work. But it is strange because I have FirstController which use FirstService (which use logger too but doesn't throw an error) which use SecondService (which use logger and throw an error).

I think that it is better to use my repository than pass code here: https://github.com/baleksandr48/webpack_for_nestjs

For running use:

npm run bundle && node -e "require('./bundled/backend.js').handler()"

Please, help me to solve this strange issue

3
  • Did you have a look at this answer: stackoverflow.com/a/59689862/4694994 and the Github issues linked in it?
    – Kim Kern
    Commented Feb 9, 2020 at 13:26
  • 1
    Kim Kern, yes I looked at this post when I created my webpack.config.
    – Farad
    Commented Feb 9, 2020 at 13:51
  • Might not answer the logger question, but nestjs does have official docs for how you're intended to integrate webpack - docs.nestjs.com/v8/faq/serverless
    – chrismarx
    Commented Sep 2, 2023 at 17:58

0