sboutzen sboutzen - 3 months ago 107
Node.js Question

Webpack `the request of a dependency is an expression`

I am testing my new setup, and for that I am compiling a server side script which looks like this:

import mongoose from 'mongoose';
import User from './models/UserSchema.jsx';

var connStr = 'mongodb://localhost:27017/mongoose-bcrypt-test';
mongoose.connect(connStr, function(err) {
if (err) throw err;
console.log('Successfully connected to MongoDB');
});

// create a user a new user
var testUser = new User({
username: 'jmar777',
password: 'Password123'
});

// save user to database
testUser.save(function(err) {
if (err) throw err;

// attempt to authenticate user
User.getAuthenticated('jmar777', 'Password123', function(err, user, reason){
if (err) throw err;

// login was successful if we have a user
if (user) {
// handle login success
console.log('login success');
return;
}

// otherwise we can determine why we failed
var reasons = User.failedLogin;
switch (reason) {
case reasons.NOT_FOUND:
case reasons.PASSWORD_INCORRECT:
// note: these cases are usually treated the same - don't tell
// the user *why* the login failed, only that it did
break;
case reasons.MAX_ATTEMPTS:
// send email or otherwise notify user that account is
// temporarily locked
break;
}
});
});


I am using webpack, with this configuration:

module.exports = {
entry: './testing.js',
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
query: {
plugins: ['./build/babelRelayPlugin'],
},
test: /\.jsx?$/
},
{
loader: 'node-loader',
test: /\.node$/,
},
{
loader: 'json-loader',
test: /\.json$/,
},
{
loader: 'raw-loader',
test: /\.md/,
},
]
},
output: {
filename: 'app.js',
path: __dirname
},
// resolve: {
// extensions: ["", ".js", ".jsx", ".node", ".webpack-loader.js", ".web-loader.js", ".loader.js"]
// },
// node: {
// fs: "empty",
// net: "empty",
// tls: "empty"
// },
target: "node"
}


When I compile the code, I get these warnings:

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

WARNING in ./~/bcrypt/~/bindings/bindings.js
Critical dependencies:
76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression
@ ./~/bcrypt/~/bindings/bindings.js 76:22-40 76:43-53

WARNING in ./~/mongoose/~/bson/lib/bson/index.js
Critical dependencies:
20:16-29 the request of a dependency is an expression
44:18-31 the request of a dependency is an expression
71:19-32 the request of a dependency is an expression
@ ./~/mongoose/~/bson/lib/bson/index.js 20:16-29 44:18-31 71:19-32

WARNING in ./~/mongodb/~/es6-promise/dist/es6-promise.js
Module not found: Error: Cannot resolve module 'vertx' in C:\bifrostApp\node_modules\mongodb\node_modules\es6-promise\dist
@ ./~/mongodb/~/es6-promise/dist/es6-promise.js 132:20-30

WARNING in ./~/mongodb/~/mongodb-core/~/bson/lib/bson/index.js
Critical dependencies:
20:16-29 the request of a dependency is an expression
44:18-31 the request of a dependency is an expression
71:19-32 the request of a dependency is an expression
@ ./~/mongodb/~/mongodb-core/~/bson/lib/bson/index.js 20:16-29 44:18-31 71:19-32


After googling around, I found this
https://github.com/webpack/webpack/issues/196
which seems to say its not a deal breaker. Still not entirely sure if it is.
The script compiles without errors, only those warnings. One piece of code generating one of the warnings is this:

/*!
* ignore
*/

var driver;

if (typeof window === 'undefined') {
driver = require(global.MONGOOSE_DRIVER_PATH || './node-mongodb-native');
} else {
driver = require('./browser');
}

/*!
* ignore
*/

module.exports = driver;


When I run the script with node, I get this error:

Error: Cannot find module './node-mongodb-native'.
at C:\bifrostApp\app.js:2165:42
at webpackContextResolve (C:\bifrostApp\app.js:2165:90)
at webpackContext (C:\bifrostApp\app.js:2162:30)
at Object.map../SPEC.md (C:\bifrostApp\app.js:2120:35)
at __webpack_require__ (C:\bifrostApp\app.js:20:30)
at Object.<anonymous> (C:\bifrostApp\app.js:832:17)
at __webpack_require__ (C:\bifrostApp\app.js:20:30)
at Object.<anonymous> (C:\bifrostApp\app.js:125:15)
at __webpack_require__ (C:\bifrostApp\app.js:20:30)
at Object.<anonymous> (C:\bifrostApp\app.js:112:19)


./node-mongodb-native is a directory, and the error comes from
webpackContextResolve(req)
with the above path as
req
:

var map = {
"./SPEC.md": 6,
"./browser/ReadPreference": 7,
"./browser/ReadPreference.js": 7,
"./browser/binary": 8,
"./browser/binary.js": 8,
"./browser/index": 31,
"./browser/index.js": 31,
"./browser/objectid": 32,
"./browser/objectid.js": 32,
"./index": 4,
"./index.js": 4,
"./node-mongodb-native/ReadPreference": 33,
"./node-mongodb-native/ReadPreference.js": 33,
"./node-mongodb-native/binary": 134,
"./node-mongodb-native/binary.js": 134,
"./node-mongodb-native/collection": 135,
"./node-mongodb-native/collection.js": 135,
"./node-mongodb-native/connection": 225,
"./node-mongodb-native/connection.js": 225,
"./node-mongodb-native/index": 231,
"./node-mongodb-native/index.js": 231,
"./node-mongodb-native/objectid": 232,
"./node-mongodb-native/objectid.js": 232
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
};
function webpackContextResolve(req) {
return map[req] || (function() { throw new Error("Cannot find module '" + req + "'.") }());
};


The path to the ./node-mongodb-native folder will then not be there, which makes sense, because the files in
map
will be executed with .call later on. My question is, why does that folder end up with a list of file paths? And what do I do to fix this? Im thinking its a webpack issue, just not quite sure what to do about it. Any help would be greatly appreciated.

Answer

I was trying to run my script with node. Running it with babel-node fixed it.