title: Connecting to MongoDB using Mongoose in multiple files
body: <p>I'm new to using mongoose and MongoDB. I'm developing a React-Redux app for saving Todo lists. I'm using webpack to bundle everything.</p>
  
  <p>What is the best way to connect to my MongoDB in multiple files? For example, when my initial app.jsx runs, and then in my reducers.jsx file to save to the database...</p>
  
  <p>I know that mongoose maintains a persistent connection, but whenever I try to use </p>
  
  <p><code>var mongoose = require('mongoose');</code></p>
  
  <p>I get this webpack error </p>
  
  <p><code>WARNING in ./~/mongoose/lib/drivers/index.js
  Critical dependencies:
  8:11-74 the request of a dependency is an expression
   @ ./~/mongoose/lib/drivers/index.js 8:11-74</code></p>
  
  <p>When i create a separate test app, I can successfully write to MongoDB using </p>
  
  <p><code>mongoose.connect() and the .save() methods,</code> when everything is kept in one file.</p>
  
  <p>Thanks</p>
  
  <p>App.jsx</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var React = require ('react');
  var ReactDOM = require ('react-dom');
  var {Route, Router, IndexRoute, hashHistory} = require ('react-router');
  
  var {Provider} = require('react-redux');
  
  var TodoApp = require('TodoApp');
  
  var actions = require('actions');
  var store = require('configureStore').configure();
  var TodoAPI = require('TodoAPI');
  
  
  // Connect to MongoDB using mongoose
  var url = 'mongodb://joe:test@ds147487.mlab.com:47487/reacttodo';
  var mongoose =require('mongoose');
  mongoose.connect(url);
  
  
  store.subscribe(()=&gt;{
    var state = store.getState();
    console.log('new state',state);
    TodoAPI.setTodos(state.todos);
  });
  
  var initialTodos = TodoAPI.getTodos();
  store.dispatch(actions.addTodos(initialTodos));
  
  // load foundation
  $(document).foundation();
  
  //app css
  require('style!css!sass!AppStyles');
  
  ReactDOM.render(
    &lt;Provider store={store}&gt;
      &lt;TodoApp/&gt;
    &lt;/Provider&gt;,
    document.getElementById('app')
  );</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>