Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error using Schema in browser: __webpack_require__(...) is not a function #5842

Closed
raythree opened this issue Nov 22, 2017 · 3 comments
Closed
Assignees
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Milestone

Comments

@raythree
Copy link

raythree commented Nov 22, 2017

We have a react-slingshot project that uses Mongoose.schema in the browser for form validation. We have this in our webpack config as per the docs:

new webpack.DefinePlugin({ 'typeof window': '\"object\"' })

Everything worked fine when we started the project using mongoose 4.11.5. After upgrading to mongoose 4.13.4 the project no longer runs in dev mode (npm start) generating this error:

embedded.js?9a85:7 Uncaught TypeError: __webpack_require__(...) is not a function

From inside one of the mongoose files. It occurs on the first line shown below when requiring the Document:

var Document = require('../document_provider')();
var EventEmitter = require('events').EventEmitter;
var PromiseProvider = require('../promise_provider');

/**
 * EmbeddedDocument constructor.
 *
 * @param {Object} obj js object returned from the db
 * @param {MongooseDocumentArray} parentArr the parent array of this document
 * @param {Boolean} skipId
 * @inherits Document
 * @api private
 */

function EmbeddedDocument(obj, parentArr, skipId, fields, index) {
 ...

Initially we didn't lock down the version of mongoose inside package.json and this started happening after version 4.11.5. I tried with the latest and it still persists. Locking the mongoose version at 4.11.5 works fine. Any ideas what changes would have caused this?

Thanks!

@vkarpov15
Copy link
Collaborator

I don't know unfortunately, we don't really have good support for webpack ATM. @varunjayaraman is the resident expert on the interplay with mongoose and webpack.

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Nov 30, 2017
@vkarpov15 vkarpov15 added this to the 4.13.6 milestone Nov 30, 2017
@raythree
Copy link
Author

raythree commented Dec 2, 2017

Yeah that makes sense, as mongoose is primarily backend. Hopefully it's easy to workout because your decision to support schema validation in the browser is great for DRY in isomorphic js apps, and most using React on the front-end use Webpack.

@vkarpov15 vkarpov15 modified the milestones: 4.13.6, 4.13.7 Dec 2, 2017
@sobafuchs
Copy link
Contributor

sobafuchs commented Dec 5, 2017

hey @raythree sorry ive been on vacation, anyway this is a problem that unfortunately creeps up from time to time with mongoose. I've been able to reproduce your issue with a simple repro script

webpack.config.js

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: './src/client.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new webpack.DefinePlugin({ 'typeof window': '\"object\"' })
  ]
}

src/client.js

import mongoose from 'mongoose';

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
  </head>

  <body>
    <div>hi</div>
    <script src="dist/bundle.js"></script>
  </body>
</html>

package.json

{
  "name": "gh-5842",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^4.13.6",
    "webpack": "^3.10.0"
  }
}

I'll need to do some more research, but I do get the same error message as you in the console when i open up index.html after bundling up the app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue
Projects
None yet
Development

No branches or pull requests

3 participants