Andry
Andry

Reputation: 16855

Underscore undefined when using Two.js and Webpack

I am new to webpack and I am trying to write a web application using webpack and two.js. For completeness, these are my dependencies:

"devDependencies": {
  "jasmine-core": "^2.8.0",
  "karma": "^1.7.0",
  "karma-chrome-launcher": "^2.2.0",
  "karma-jasmine": "^1.1.0",
  "karma-webpack": "^2.0.4",
  "webpack": "^3.5.5"
},
"dependencies": {
  "two.js": "^0.6.0"
}

I have the following webpack.config.js:

const path = require('path');
module.exports = {
    entry: './myentry.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'out')
    }
};

File myentry.js just imports two.js:

import two from "two.js";

Bundling succeeds...

Library two.js is part of my npm dependencies, so it can be found among my local node modules in node_modules folder. When I go ahead and create the bundle:

webpack --config webpack.config.js

It succeeds and I get the following output:

Hash: 5e762def59fa65ff8487
Version: webpack 3.5.5
Time: 468ms
               Asset    Size  Chunks                    Chunk Names
bundle.js  258 kB       0  [emitted]  [big]  main
   [0] ./myentry.js 271 bytes {0} [built]
    + 1 hidden module

The generated bundle is available here.

But... running fails:

So I use the bundle in my HTML page:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My stuff</title>
    <script type="application/javascript" src="./out/bundle.js"></script>
</head>
<body></body>
</html>

When I open the page in Chrome (I use http-server to avoid CORS related issues), I get these errors in F12 Tools:

Uncaught TypeError: Cannot read property 'isFunction' of undefined
    at bundle.js:1917
    at Object.<anonymous> (bundle.js:3867)
    at __webpack_require__ (bundle.js:20)
    at Object.root (bundle.js:72)
    at __webpack_require__ (bundle.js:20)
    at Object.defineProperty.value (bundle.js:63)
    at bundle.js:66

If you inspect the bundle, you will see the error is on this line:

hasEventListeners: _.isFunction(root.addEventListener),

So underscore is not defined, but it is provided inside two.js! Why is it a problem with webpack?

Upvotes: 3

Views: 387

Answers (0)

Related Questions