After attempting to integrate three.js into my code as a standalone script without using webpack, browserify or requirejs, I encountered an issue. My setup involves loading an external Angular app and extending it, but now I need my own version of THREE.js within the codebase.
Although the library gets loaded, the first line tries to set a variable 'global' which appears to be undefined. What could I be overlooking?
// edit:
I am utilizing a JavaScript API from another company. It's unclear whether they define the 'global' variable, but Three.js seems to rely on it even though I'm not using it in a node setup. In all the examples, it typically works as a drop-in script.
If I switch to the minified version, the error message changes to
TypeError: global is undefined *three.min.js:2:168
anonymous https://localhost:9000/scripts/three.min.js:2:168
anonymous https://localhost:9000/scripts/three.min.js:2:2*
This issue arises from the initial lines of the three.js file:
function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.THREE = {}))); }(this, (function (exports) { 'use strict'; ...
//EDIT 2:
I have finally identified the cause of this error. When using gulp-babel and including scripts with that snippet at the top, Babel attempts to replace 'THIS' with the current context, which is - unsurprisingly - undefined. This results in Babel literally replacing 'this' with 'undefined'. Therefore, never run babel() on your final vendor files!