typescript - 与 webpack 捆绑后 Nestjs 应用程序无法运行
问题描述
我有一个 nestjs 应用程序,我使用 webpack 将整个项目捆绑到一个 main.js 文件中。我的 webpack 配置文件刚刚通过这个 repo流动,但是当我运行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.
nodejs版本: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
]
}
};
那么有人可以告诉我为什么我有这个错误吗?
解决方案
对于未在 Nest 的 @Prop 注释中明确声明其类型的 Mongoose 对象模型中的属性,我遇到了这个问题。它必须是一个类,而不是一个接口。像这样:
class InnerPropExample {
name: string
}
@Schema()
export class MongooseModelExample extends Document {
@Prop(InnerPropExample)
innerPropExample: InnerPropExample;
}
至于它发生的原因,Nest.js 文档只是提到:
“借助 TypeScript 元数据(和反射)功能,可以自动推断这些属性的模式类型。但是,在无法隐式反映类型的更复杂场景中(例如,数组或嵌套对象结构),必须明确指示类型”
.
推荐阅读
- javascript - 从父元素传递道具
- java - 如何
使用 Selenium 和 Java 将字符串拆分为列表 - mongodb - 我应该在 mongodb 事务中创建用于重试的递归方法以避免文档级锁定错误吗?
- file - 为摘要使用单独的文件
- java - 如何在 Java 中为 Math.Context 设置比例?
- angular - 使用 controlValueAccessor 的自定义组件 - ts-Lint 错误:在定义之前使用了组件
- android - 使用 useSafeArea Hook 时反应原生 Android TextInput 问题
- android - 如何在flutter android中将文本添加到默认启动画面
- c++ - XCB:xcb_wait_for_event 未捕获应用程序生成的事件
- java - 插入一对多关系弹簧靴