Reputation: 317
I am trying to include mongoose/mongodb in my Express.JS app (the ES2015 way).
First, I have the main file:
import path from 'path'
import express from 'express'
import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost/timelines')
const app = express()
app.use(express.static('dist/client/'))
app.get('*', (req, res) => {
res.sendFile(path.resolve('dist/client/index.html'))
})
app.listen(3000)
Then the error message:
[nodemon] starting `node dist/server/app.js`
undefined:8
driver = !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND';; throw e; }());
^
Error: Cannot find module "."
My webpack config:
export default [
{
entry: './src/server/app.js',
target: 'node',
output: {
path: './dist/server',
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
}
},
{
entry: './src/client/app.js',
output: {
path: './dist/client',
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'stage-2', 'react']
}
}
]
}
}
]
Before I added
import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost/timelines')
everything worked fine!!
Any thoughts how this problem can be fixed?
webpack throws a warning:
WARNING in ./~/es6-promise/dist/es6-promise.js
Module not found: Error: Can't resolve 'vertx' in '/Users/timo/Desktop/Timelines/node_modules/es6-promise/dist'
@ ./~/es6-promise/dist/es6-promise.js 131:20-30
@ ./~/mongodb/lib/mongo_client.js
@ ./~/mongodb/index.js
@ ./~/mongoose/lib/index.js
@ ./~/mongoose/index.js
@ ./src/server/app.js
Upvotes: 1
Views: 868
Reputation: 65
I ran into the same issue, and there were a few key steps in order to resolve it.
Firstly, please check that you have the version of MongoDB server installed that Mongoose is expecting: Mongoose MongoDB NodeJS driver compatibility
The root cause of the issue is that Webpack is trying to resolve a dynamic require to load the MongoDB NodeJS driver:
if (typeof window === 'undefined') {
driver = require(global.MONGOOSE_DRIVER_PATH || './node-mongodb-native');
} else {
driver = require('./browser');
}
We need to instruct webpack to ignore all external imports as we don't need to bundle them, since they will never be sent down to the client.
We can do that by adding the following webpack config:
module.exports = {
// tell webpack that it is building for the NodeJS environment
...
target: 'node',
output: {
...
// we want the output to use simple require calls for imports as
// nodejs would expect
libraryTarget: 'commonjs'
},
...
externals: [
// consider everything imported from a non-relative path an external
// this will flag everything such as:
// import express from 'express'
// or
// import mongoose from 'mongoose'
/^(?!\.|\/).+/i
]
}
When you run your build again and start up the resultant bundle, you shouldn't receive the cryptic error message from Webpack any longer and find that your server is now running as expected.
Upvotes: 2
Reputation: 111446
I think you're missing a semicolon. Just kidding. (But seriously, if you're not using semicolons then you will experience problems if you're not 100% sure what you're doing. At least lint your code according to eslint-config-npm to make sure that you're not missing a semicolon where it actually matters.)
Make sure that you installed the mongoose module correctly with its dependencies and that you include everything needed in webpack. It looks like some code is trying to import '.' and cannot do it - for example there's no package.json or incomplete package.json there.
Upvotes: 0