What is the best way to add extra plugins to a library that has already been imported through JSPM?

After successfully importing a 3D rendering library with JSPM, I encountered an issue while trying to import the Orbit Controls plugin for Three.js

import THREE from 'three.js/build/three';
import OrbitControls from 'three.js/examples/js/controls/OrbitControls';

Unfortunately, importing the plugin directly threw an error as it had no reference to the main library

Uncaught ReferenceError: THREE is not definedOrbitControls.js:24 (anonymous function)system.src.js:2187 doEvalsystem.src.js:2153 __evalsystem.src.js:212 asystem.src.js:1019 global.e.metadata.format.e.metadata.executesystem.src.js:540 csystem.src.js:403 ssystem.src.js:715 executees6-module-loader.src.js:1879 oes6-module-loader.src.js:1927 pes6-module-loader.src.js:1701 jes6-module-loader.src.js:1749 kes6-module-loader.src.js:1575 (anonymous function)es6-module-loader.src.js:1177 Oes6-module-loader.src.js:1136 Kes6-module-loader.src.js:927 y.whenes6-module-loader.src.js:818 v.runes6-module-loader.src.js:97 a._draines6-module-loader.src.js:62 draines6-module-loader.src.js:267 b
es6-module-loader.src.js:139 Potentially unhandled rejection [2] Error loading "github:mrdoob/three.js@master/examples/js/controls/OrbitControls" at http://localhost:8080/jspm_packages/github/mrdoob/three.js@master/examples/js/controls/OrbitControls.js
Error loading "github:mrdoob/three.js@master/examples/js/controls/OrbitControls" from "app/main" at http://localhost:8080/app/main.js
Error evaluating http://localhost:8080/jspm_packages/github/mrdoob/three.js@master/examples/js/controls/OrbitControls.js
Uncaught ReferenceError: THREE is not defined (WARNING: non-Error used)

The plugin is supposed to enhance the library's functionality by adding more features like so:

THREE.OrbitControls = function ( object, domElement ) { ... }

I'm now looking for the correct method to import the plugin smoothly without encountering any errors

Answer №1

When it comes to the current installation of NPM for three.js (version 0.81.2):

There is a bundle file named three.js

import * as THREE from 'three'; // the build/three.js file from the node_modules/three directory
window.THREE = THREE;
require('three/examples/js/controls/OrbitControls.js');
export default THREE;

Next, in your index.js file:

import THREE from './three';

Answer №2

This particular scenario involves the initialization of THREE within its own module scope (ModuleEnvironment, details can be found here). It is not being exported and is causing an issue because OrbitControls is searching for THREE within its module scope and then in the global scope. As a result, THREE is not located, leading to an error being thrown.

Here are a couple of ways to address this problem:

  1. If you are coding for browsers, you can opt not to utilize the es6 module system for libraries that do not support it. Instead, load them via script tags and use them as global variables (the traditional method pre-es6).

  2. Another option is to employ system.js. In this scenario, you would need to configure your System as follows:

    System.map['TREE'] = 'three.js/build/three';
    System.meta['three.js/build/three'] = { format: 'global', exports: 'jQuery' };
    System.meta['three.js/examples/js/controls/OrbitControls'] = { deps: 'TREE' }
    

    You can then import these modules like so:

    import THREE from 'THREE';
    import 'three.js/examples/js/controls/OrbitControls';
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing Typography in Material UI with Custom Breakpoints in React CustomThemes

Currently, I am utilizing material UI and React to develop a single-page application (SPA). However, I have encountered an issue with ensuring that my pages are responsive for smaller screen sizes. To address this, I have been manually adding fontSize: { x ...

What is the reason for using a wrapper with fs.readFile when a callback is included as an argument?

Recently delving into Node.js, I encountered a perplexing scenario while using fs.readFile(). Initially, my attempt to read a file led me to use: fs.readFile("file.txt",function(err,data) { if(err) {throw err;} console.log(data); }); However, to ...

Deactivate JQuery star rating plugin after user has voted

I've integrated the JQuery star rating plugin (v2.61) from into my project. Everything is working smoothly, but I am looking for a way to disable the stars once a user has voted. Currently, users are able to select their rating and submit it through ...

Vuejs unstyled content flash

I encountered an issue while loading a page with Vue. Initially, I am able to access variables like @{{ value }} but once the page is fully loaded, the variable becomes invisible. How can I resolve this issue? I have already included Bootstrap and all scri ...

Include a for loop in the line graph on Google Charts

I need help figuring out how to use a for loop to iterate over data in order to populate my Google Chart. The code snippet below outlines what I've already tried. var line_div = '2016-08-04,4|2016-08-05,7|2016-08-06,9|2016-08-07,2'; var lin ...

Elevate the value of a particular element's variable through the execution of a function

By using a for loop, I was able to generate a variable number of divs that change their background color individually when hovered over with the mouse. Now, I want to implement a function that will decrease the brightness of each div by 10% every time it i ...

Retrieve the data stored in a JSON object

After making an ajax call, the json object that gets returned looks like this: Object {0: "1", 1: "jake", 2: "#00ff00", tip_id: "1", tip_details: "jake", tip_color: "#00ff00"} Object {0: "2", 1: "jakee", 2: "#00ff00", tip_id: "2", tip_details: "jakee", t ...

Incorporate a personalized style into the wysihtml5 text editor

Is there a way for me to insert a button that applies a custom class of my choice? I haven't been able to find this feature in the documentation, even though it's a commonly requested one. Here's an example of what I'm looking for: If ...

Slider Jquery - Displaying Half-Step Visual Bar Lengths

JSFIDDLE $(function() { $( "#slider-range-min" ).slider({ range: "min", value: 5, min: 0, step: .5, max: 10, slide: function( event, ui ) { $( "#amount" ).val(ui.value); ...

Contemplating the Sequence of DOM Execution

In the world of html/javascript, the order of execution is typically serial, meaning that the browser reads the code line by line and interprets it accordingly. This is a common practice in most programming languages. Some javascript programmers prefer to ...

Implementing a Retrofit Null Response in the onSuccess Method

While watching a tutorial on Retrofit for sending objects in a post request on YouTube, I encountered an error on the emulator with a null response in the onResponse method. Problem: https://i.sstatic.net/t6mnw.gif Main Activity: public class MainActivi ...

Using CSS to automatically adjust the width of a child div based on its content, rather than stretching it to fill the entire

I'm currently facing an issue with my CSS. Apologies for the vague title, but I'll do my best to explain it here. My problem lies within a parent wrapper div that is centered on the page. Within this wrapper, there is another child div containing ...

The word "yargs" will not activate a command within a script

I have a NodeJs script that requires parsing command line arguments. Here is the code I have written: import yargs from "yargs"; import { hideBin } from 'yargs/helpers'; //.... yargs(hideBin(process.argv)).command('--add-item ...

Incorporate object keys into an array using JavaScript

Query: I'm working on a JavaScript project and I have an array that looks like this: [6.7, 8, 7, 8.6]. I need to transform this array into an array of objects with named properties: [{y: 6.7} , {y: 8}, {y: 7}, {y: 8.6}]. Can someone guide me on how to ...

I've mastered using 'for' loops to draw the tree, but I'm now eager to challenge myself with utilizing recursion for the job

In the past, I used for loops to draw a tree (see picture one), but now I want to switch to using recursion to draw the tree. Can someone show me how to change the writing style from for loops to recursive functions? https://i.sstatic.net/9kndi.jpg Begi ...

Attempting to modify the information within the #content division of a webpage through the activation of a linked image within a jquery slideshow

I'm completely stuck on this one, even though I'm relatively new to jquery. My goal is to design a webpage where there is a slideshow of products at the top, and when a user clicks on one of the products, it should update the main #content div w ...

What could be the reason for the container div's height not being properly refreshed?

When adding elements to a container div with an initial height of 'auto', I assumed that the height would adjust based on the children elements added. However, this is not happening. Can anyone assist me in ensuring that the container div's ...

Removing a property from a JSON object when initiating an Ajax request in JavaScript

Looking for guidance on JavaScript and ajax as a beginner. I have a JSON with an output cell that I want to remove: { "cells": [{ "metadata": { "trusted": true, "collapsed": false }, ...

Struggling to get the ReactJS.NET MVC tutorial to function properly?

After deciding to start a new project in Visual Studio with MVC 5 and a single page app using ReactJS, I consulted the guide on the ReactJS website. Upon running the project for the first time, I encountered a syntax error due to JSX. It seemed that the b ...

Tips for inserting a property into an array of objects when it matches the specified ID

As someone new to Angular, I am encountering an issue and would appreciate some help. Here is an array of objects I am working with: signals = [{ 'signalID': '123' },{ 'signalID': '233' },{ 'signalID': &apo ...