使用 webpack 打包后,Nestjs 应用无法运行

3
我有一个nestjs应用程序,我使用webpack将整个项目打包到main.js文件中。我的webpack配置文件只是参考这个仓库,但当我运行npm run build:prod将main.js打包到dist/main.js,并运行node main.js时,有时会抛出我从未见过的错误。像这样:
throw new errors_1.CannotDetermineTypeError((_a = target.constructor) === null || _a === void 0 ? void 0 : _a.name, propertyKey);
                ^
CannotDetermineTypeError: Cannot determine a type for the "n.operation" field (union/intersection/ambiguous type was used). Make sure your property is decorated with a "@Prop({ type: TYPE_HERE })" decorator.

Node.js版本:v12.18.4

NestJS /核心版本:7.0.0

Webpack-cli 版本:4.2.0

我的Webpack配置

const path = require('path');
const webpack = require('webpack');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

const { NODE_ENV = 'none' } = process.env;

console.log(`-- Webpack <${NODE_ENV}> build --`);

module.exports = {
  entry: './src/main.ts',
  mode: NODE_ENV,
  target: 'node',
  plugins: [
    new webpack.IgnorePlugin({
      checkResource(resource) {
        const lazyImports = [
          '@nestjs/microservices',
          '@nestjs/platform-express',
          'cache-manager',
          'class-validator',
          'class-transformer'
        ];
        if (!lazyImports.includes(resource)) {
          return false;
        }
        try {
          require.resolve(resource);
        } catch (err) {
          return true;
        }
        return false;
      }
    })
  ],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  },
  resolve: {
    extensions: ['.ts', '.js'],
    plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.build.json' })]
  },
  module: {
    rules: [{ test: /\.ts$/, loader: 'ts-loader' }]
  },
  stats: {
    warningsFilter: [
      'node_modules/express/lib/view.js',
      'node_modules/@nestjs/common/utils/load-package.util.js',
      'node_modules/@nestjs/core/helpers/load-adapter.js',
      'node_modules/mongoose/lib/index.js',
      'node_modules/mqtt/node_modules/ws/lib/buffer-util.js',
      'node_modules/mqtt/node_modules/ws/lib/validation.js',
      'node_modules/mongodb/lib/operations/connect.js',
      'node_modules/grpc/src/grpc_extension.js',
      'node_modules/bytebuffer/dist/bytebuffer-node.js',
      'node_modules/@nestjs/core/helpers/optional-require.js',
      'node_modules/require_optional/index.js',
      'node_modules/node-pre-gyp/lib/util/versioning.js',
      'node_modules/node-pre-gyp/lib/pre-binding.js',
      'node_modules/ws/lib/buffer-util.js',
      'node_modules/ws/lib/validation.js',
      warning => false
    ]
  }
};

请问有人能告诉我为什么会出现这个错误吗?

2个回答

4

我在Mongoose对象模型的属性中遇到了这个问题,它没有在Nest的@Prop注释中显式声明其类型。而且它必须是一个类,而不是接口。

class InnerPropExample {
    name: string
}

@Schema()
export class MongooseModelExample extends Document {
  @Prop(InnerPropExample)
  innerPropExample: InnerPropExample;
}

关于为什么会发生这种情况,Nest.js文档只是提到:
“由于TypeScript元数据(和反射)功能的自动推断,这些属性的模式类型被自动推断。然而,在更复杂的情况下,无法隐式反映类型(例如,数组或嵌套对象结构),必须明确指出类型。”

0

我也遇到了这个问题,我用以下提示修复了它:@Prop({ type: TYPE_HERE })。就像这样:

interface MassageContent {
  txt?: string
}

@Prop({ require: true, type: Object })
  content: MassageContent;

将接口改为类不如这样做更好


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接