'Nestjs application can not run after bundled with webpack
I have a nestjs app and i use webpack to bundled this entire project to a main.js file. My webpack configure file just flowed by this repo ,but when i run npm run build:prod
to bundled main.js to dist/main.js and run node main.js
it sometimes throw an error i never seen before.
like this:
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 version: v12.18.4
nestjs/core version: 7.0.0
webpack-cli version: 4.2.0
my webpack config
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
]
}
};
So can someone tell me why i had this error?
Solution 1:[1]
I had this problem for properties in Mongoose object models that did not explicitly declare its types in Nest's @Prop annotation. And it must be a class, not an interface. Like so:
class InnerPropExample {
name: string
}
@Schema()
export class MongooseModelExample extends Document {
@Prop(InnerPropExample)
innerPropExample: InnerPropExample;
}
As for the reason why it happens, Nest.js doc just mentions that:
"The schema types for these properties are automatically inferred thanks to TypeScript metadata (and reflection) capabilities. However, in more complex scenarios in which types cannot be implicitly reflected (for example, arrays or nested object structures), types must be indicated explicitly"
.
Solution 2:[2]
I had this problem too, and I fix it with that prompt @Prop({ type: TYPE_HERE })
. Like so:
interface MassageContent {
txt?: string
}
@Prop({ require: true, type: Object })
content: MassageContent;
it is better than changing the interface to a class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Petter |
Solution 2 | Piotr Labunski |