Skip to content

[Bug]: Struggling with nestjs #10393

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

Open
MIreland opened this issue May 17, 2025 · 1 comment
Open

[Bug]: Struggling with nestjs #10393

MIreland opened this issue May 17, 2025 · 1 comment
Labels
pending triage The issue/PR is currently untouched.

Comments

@MIreland
Copy link

System Info

  System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.20 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.0/bin/npm
    pnpm: 9.12.3 - ~/.nvm/versions/node/v20.18.0/bin/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 136.0.7103.114
    Safari: 17.6
  npmPackages:
    @rspack/cli: catalog: => 1.3.8 
    @rspack/core: catalog: => 1.3.8 

Details

I'm struggling to set up nestjs in my local app.

I see a lot of issues that look like this:

Image

I also saw a lot of issues that look like this:

Image

Inspired by #2741

I created an rspack config that looks like this:

const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
const { rspack } = require('@rspack/core');

/** @type {import('@rspack/cli').Configuration} */
const config = {
    context: __dirname,
    target: 'node',

    entry: {
        main: ['@rspack/core/hot/poll?100', './src/main.ts'],
    },
    resolve: {
        extensions: ['...', '.ts', '.tsx', '.jsx'],
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: {
                    loader: 'builtin:swc-loader',
                    options: {
                        jsc: {
                            parser: {
                                syntax: 'typescript',
                                decorators: true,
                            },
                            transform: {
                                legacyDecorator: true,
                                decoratorMetadata: true,
                            },
                        },
                    },
                },
            },
        ],
    },
    optimization: {
        minimize: false,
        minimizer: [
            new rspack.SwcJsMinimizerRspackPlugin({
                minimizerOptions: {
                    // We need to disable mangling and compression for class names and function names for Nest.js to work properly
                    // The execution context class returns a reference to the class/handler function, which is for example used for applying metadata using decorators
                    // https://docs.nestjs.com/fundamentals/execution-context#executioncontext-class
                    compress: {
                        keep_classnames: true,
                        keep_fnames: true,
                    },
                    mangle: {
                        keep_classnames: true,
                        keep_fnames: true,
                    },
                },
            }),
        ],
    },
    externalsType: 'commonjs',
    plugins: [
        !process.env.BUILD &&
            new RunScriptWebpackPlugin({
                name: 'main.js',
                autoRestart: false,
            }),
    ].filter(Boolean),
    devServer: {
        devMiddleware: {
            writeToDisk: true,
        },
    },
    externals: [
        function (obj, callback) {
            const resource = obj.request;
            const lazyImports = [
                '@nestjs/core',
                '@nestjs/microservices',
                '@nestjs/platform-express',
                'cache-manager',
                'class-validator',
                'class-transformer',
                // ADD THIS
                '@nestjs/microservices/microservices-module',
                '@nestjs/microservices/server',
                '@nestjs/websockets',
                'amqplib',
                'nats',
                'socket.io-adapter',
                'utf-8-validate',
                'bufferutil',
                'kerberos',
                '@mongodb-js/zstd',
                'snappy',
                '@aws-sdk/credential-providers',
                'mongodb-client-encryption',
                '@nestjs/websockets/socket-module',
                'bson-ext',
                'snappy/package.json',
                'aws4',
                'lua',
                'sharp',
                'amqp-connection-manager',
                'swc-loader',
                'node-gyp',
                'pg-native',
                'loader-utils',
                'fsevents',
                'kafkajs',
                'mqtt',
                '@swc/wasm',
                '@swc/core',
                '@swc/core-darwin-arm64',
                'swc+core-darwin-arm64',
                '@apollo/gateway',
                'apollo-server-fastify',
                'mock-aws-s3',
                '@mapbox+node-pre-gyp',
                'ts-morph',
            ];
            const requireImports = ['node-gyp', 'shard', 'sharp', 'fsevents'];
            if (requireImports.includes(resource)) {
                require.resolve(resource);
                callback(null, resource);
                return;
            }

            if (!lazyImports.includes(resource)) {
                return callback();
            }
            try {
                require.resolve(resource);
            } catch (err) {
                callback(null, resource);
            }
            callback();
        },
    ],
};
module.exports = config;

Reproduce link

https://github.com/MIreland/nestjs-rspack

Reproduce Steps

  1. check out that repo
  2. pnpm install
  3. pnpm dev
Image
@MIreland MIreland added the pending triage The issue/PR is currently untouched. label May 17, 2025
@MIreland
Copy link
Author

Also, how do I know what I should/shouldn't be putting in lazyImports in that config?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

1 participant