Importing three.js using ES6 syntax

When it comes to working with ES6, my workflow involves using Babel and babel-plugin-transform-es2015-modules-system.js specifically to transform module import/export for compatibility with system.js. I rely on a "green" browser for most ES6 features except for modules importing/exporting, which are a whatwg standard and not technically considered "ES6".

This setup works seamlessly with non-ES6 libraries, allowing me to easily "import" all the necessary npm packages. However, when it comes to three.js, things get a bit trickier. Despite trying different releases like three.js, three.min.js, and three.modules.js, I encounter issues such as undefined modules or a requirement for traceur (possibly for a system.js-like transformation).

So, how can I successfully integrate three.js into my ES6 environment? One option could be reverting to using a <script> tag and creating a global variable for three. Alternatively, I could explore solutions like Rollup/webpack to bypass the module complications.

Yet, I believe there must be a more elegant solution out there, given that three.js itself utilizes ES6 modules internally.

Answer №1

After encountering a bug in the latest release (just one day old!), I was able to fix it but encountered further problems. However, I did discover that the following code does work:

import * as THREE from 'etc/three.js'

Unfortunately, the more straightforward versions,

import THREE from 'etc/three.js'
or
import 'etc/three.js'

do not seem to function properly.

If you have a better understanding of this issue than I do, feel free to share, as my current grasp on it feels quite random.

Answer №2

I encountered a similar issue recently, only to discover that THREE does not export THREE itself, but rather exports various modules separately. To resolve this, take a look at the section in the three.js file where the exports are listed:

exports.WebGLRenderTargetCube = WebGLRenderTargetCube;
exports.WebGLRenderTarget = WebGLRenderTarget;
exports.WebGLRenderer = WebGLRenderer;
exports.ShaderLib = ShaderLib;
exports.UniformsLib = UniformsLib;
exports.UniformsUtils = UniformsUtils;
exports.ShaderChunk = ShaderChunk;
exports.FogExp2 = FogExp2;
exports.Fog = Fog;
exports.Scene = Scene;
exports.LensFlare = LensFlare;
exports.Sprite = Sprite;
exports.LOD = LOD;
exports.SkinnedMesh = SkinnedMesh;
exports.Skeleton = Skeleton;
exports.Bone = Bone;
exports.Mesh = Mesh;
exports.LineSegments = LineSegments;
exports.Line = Line;
exports.Points = Points;
exports.Group = Group;

By importing only the necessary modules, you can streamline your code. Alternatively, as mentioned earlier: Import all modules using

 import * as THREE from 'three.js'

Cheers

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

Autofill Text Input with React Material-UI

For my current project, I am utilizing Material UI and React. Each TextField in the project has a button next to it, and when the button is clicked, I want it to retrieve the value of its corresponding TextField and set that value as the input for another ...

How do I define the specific icon to display on the splash screen for progressive web apps?

In my Progressive Web App (PWA), I have set icons at sizes 144 and 512. Although both icons appear in the application tab in Chrome, the splash screen displays a really small icon (I assume it's using the 144 icon). Is there a method to indicate which ...

Issue with incremental static generation in version 13 not functioning as expected

Is ISR working for anyone in the NextJS 13 Beta version? I have set revalidate to 15 as follows. export const revalidate = 15; However, even after running `npm run build`, the page still remains a statically generated site (SSG). The symbol is showing u ...

My goal is to monitor every action (button) that users take while using the PDF viewer

Each browser displays the view differently, and I am interested in monitoring specific user actions such as button presses on a PDF viewer. I am currently setting up an iframe and configuring its attributes. Is there a way to achieve this? ...

Achieving the functionality of keeping the search query box and alphabetical search options visible on the page even after performing a search and displaying the results can be done by

I have set up a PHP page with search query field and alphabetical search options. When submitting the query, the results are displayed on the same page; however, I would like the results to show in the same location as the alphabetical search. Currently, ...

Utilize Server Side Includes in your JavaScript code

When the query string is parsed, a specific section of code SSI is included in the footer page. Here are some examples of query strings: ?headId=520&genderType=2 ?headId=600&genderType=1 function GetQueryStringParams(sParam){ var sPageURL ...

Having trouble installing the npm module telldus-core-js

Hello, I am looking for assistance in building telldus-core-js on a Raspberry Pi. I keep encountering the error below and can't seem to figure out where I'm going wrong. The module I am attempting to install can be found here: https://github.co ...

Transferring an image between two <td> cells within the same table

For a project, I'm working on creating a Checkers game. I have the checker board and pieces ready, but I'm struggling with the movement logic. My goal is to enable the image to move or transfer from one td element to another when clicked. A frie ...

jQuery struggles to process large response

I am currently developing an application that sends requests to a URL and parses the table found in the HTML response using jQuery. While this method works well for smaller amounts of HTML code, it runs into issues with larger datasets. The problem arises ...

Are programmatic clicks distinguishable from manual clicks in JQuery?

Currently, I am in the process of creating selectable and draggable elements for a table. You can test the functionality by following this JsFiddle link (please try vertically selecting). Initially, the selection works well but upon trying to select for th ...

Click event triggers nested bootstrap collapse

As a beginner in bootstraps and coding, I am currently experimenting with opening the main bootstrap panel using an onclick event that includes nested sub panels. Here is my index.html file containing the panels and the button; <link href="https://m ...

Mistake in Timestamp Separation - JavaScript

I have a Timestamp retrieved from MSSQL and displayed on a Webclient. The timestamp reads: Thu Jan 01 18:00:00 CET 1970, however, I am only interested in the time format: 18:00 or 18:00:00 (preferably the former). <script> var timestamp = ...

What is the best way to incorporate my theme classes into my React component for custom styling?

Struggling to customize my React header buttons as I can't seem to apply my classes function that utilizes useStyles(). The error seems to be coming from: className: {classes.menuButton} within my getMenuButtons function. const useStyles = makeStyles ...

How can I locate ThreeJS coordinates within the Panoramic GUI?

While I've dabbled with ThreeJS in the past, I'm currently working on a new project that involves setting up hotspots in a panoramic view. I vaguely recall using the camera dolly tool from this link to visualize camera locations. However, I' ...

Updating my Cordova plugin collection

My Cordova plugin for Android applications differs from others as it only utilizes an Application (App.java) and not a CordovaPlugin class. Upon initial installation using cordova plugin add, everything functions correctly. However, when attempting to rem ...

HTML/JavaScript - Ways to show text entered into an input field as HTML code

One dilemma I'm facing involves a textarea element on my website where users input HTML code. My goal is to showcase this entered HTML code in a different section of the webpage. How should I approach this challenge? The desired outcome is similar to ...

Encountered a problem while trying to install angular/cli via the

Encountering errors while attempting to install Angular/CLI using the npm command line. The error message displayed is as follows: npm ERR! Darwin 16.7.0 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "@angular/cli" npm ERR! node ...

Enhancing functionality with extra JavaScript tags in next.js

Recently, I delved into programming with react & next.js after previously working with node.js. It didn't take long for me to create my first application, but upon inspecting the page, I noticed an abundance of extra JavaScript code (see image below). ...

Actions in ASP Core to automatically install necessary tools for Angular2 before and after publishing, followed by the build

Our .NET Core project utilizes Angular2 as the frontend client, housed in a Frontend directory within our solution. The Frontend directory includes package.json and angular-cli.json to keep frontend separate from the rest of the .NET project. When ng buil ...

Thymeleaf not triggering JQuery click event

Currently working on a Spring Boot site where I have a list of elements, each containing a link. The goal is to trigger a javascript function when these links are clicked. <div class="col-sm-4" th:each="product : ${productsList}"> <!-- Code... ...