I'm using Webpack to use require modules in the browser; however, I'm getting the following error:
Uncaught TypeError: mongoose.model is not a function
at Object.<anonymous> (bundle.js:44338)
at __webpack_require__ (bundle.js:20)
at Object.<anonymous> (bundle.js:48)
at Object.<anonymous> (bundle.js:68)
at __webpack_require__ (bundle.js:20)
at bundle.js:40
at bundle.js:43
I know mongoose has some limitations in the browser according to http://mongoosejs.com/docs/browser.html and I even included the script tag suggested and still receiving the error.
I have also exported my module using mongoose correctly:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
imageURL: String,
productName: String,
productType: String,
price: String
}, {collection: 'products'});
module.exports = mongoose.model('products', ProductSchema);
My webpack.config.js is configured correctly as well
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var request = require('request');
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
module.exports = {
entry: "./main.js",
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [
{ test: /.json$/, loader: 'json-loader' }
]
},
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.js', '.json']
},
node: {
console: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty'
}
}
Why is this occurring? And how can this be resolved?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…