The Three.js Scene does not function as a constructor

Trying to create a basic scene using Three.js for some experimentation, but facing an unknown issue.

The console keeps displaying an error stating that Three.Scene() is not a constructor, whether I import my own download of Three or use the cdnjs.

import * as Three from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.js';


let scene, camera, renderer, cube;

function init() {
    scene = new Three.Scene();
    camera = new Three.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

    renderer = new Three.WebGLRenderer({ antialias: true });

    renderer.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(renderer.domElement);

    const material = new Three.MeshBasicMaterial({ color: 0x00ff00 });

    scene.background = new Three.color(FFFF);

    const geometry = new Three.BoxGeometry(1, 1, 1);
    cube = new Three.Mesh(geometry, material);
    scene.add(cube);

    camera.position.z = 5;
}


function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
}

init();

window.addEventListener('resize', onWindowResize, false)


function animate() {
    requestAnimationFrame(animate);
    cube.rotation.x += 0.1;
    cube.rotation.y += 0.1;
    renderer.render(scene, camera);
}

animate();

Tried searching for a solution, but all similar issues were caused by typos or incorrect imports.

Here's the HTML code:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>THREE Test</title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <script type="module" src="./code.js"></script>
</body>
</html>

Answer №1

The correct way to import is:

https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js

three.module.js is the ESM build file while three.js is UMD. If you prefer using ES6 import syntax in your application, make sure to use the ESM version.

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

Unusual occurrences with nested Angular directives

I have been trying to implement directives within other directives, but the results are not what I expected. Can anyone help me understand this code snippet: var app = angular.module('blah', []); app.directive('one', function() { ...

Is the JavaScript click event ineffective for <select> <option>s?

It seems like there's a problem with the code below that is causing it to not work as expected. I'm trying to figure out why the onclick events on the <option>s are not triggering, but everything else seems fine. function displayInput() ...

Regular expressions are used for validation purposes

I've been using the regular expression shown below to validate certain text: ^\d+(?:fs|sf)[-+]\d+[hmd]$/ Here are some sample texts I have validated with this regular expression: 20fs-4d 10sf+20m 3fs-2h So far, it's been working we ...

Is there a way to play back the recorded sound in my JavaScript project?

I recently completed a tutorial on audio recording in JavaScript where an array is used to store the recorded audio and convert it into a URL for playback. The setup includes a button with a function that both records and automatically plays the audio when ...

Issues with jQuery functionality in Django

Having some trouble displaying HTML data stored in result on my page with this code: $("#regerror").html(result); It doesn't seem to be working. However, when I use plain Javascript it works fine: document.getElementById("regerror").innerHTML = res ...

JavaScript object conversion to Java Model is proving to be a challenge

When looking at my Spring controller and client-side Javascript code, I noticed that the Javascript object is having trouble reaching the Spring controller in Object form. Here is a snippet of my Controller code: @RequestMapping(value = "/addRating", meth ...

Error Encountered: AJAX Request Failed with 400 Bad Request

I've been using mithril.js to communicate with my node back-end. I followed the documentation and added an AJAX request, but there seems to be limited information available on mithril. Encountered Error: mithril.js:2130 POST http://localhost:3000/a ...

Clickable link unresponsive on parallax-enhanced webpage

Currently, I am utilizing Zurb foundation's Manifesto theme for creating a parallax scrolling landing page. The anchor tag is essential for the scrolling effect on this page, causing a conflict when regular anchor links are included. Here is the HTML ...

The textbox fails to update when the condition in the IF statement is met

In the following code, I have an input box with the ID volumetric_weight that gets updated on keyup. However, the second textbox with the ID volumetric_price does not update as expected, even though I believe I wrote it correctly. I am wondering if there ...

Is there a way to direct Webpack in a Next.JS application to resolve a particular dependency from an external directory?

Is it possible to make all react imports in the given directory structure resolve to react-b? |__node_modules | |__react-a | |__app-a | |__component-a | |__next-app | |__react-b | |__component-b // component-a import { useEffect } from ' ...

When sending an Axios POST request, a "Network Error" message may be received even when the status code is 200 and there is a valid response

Currently, I am utilizing Vue JS version 2.5 along with Axios: "vue": "^2.5.17", "vue-axios": "^2.1.4", "axios": "^0.18.0", The main issue I am facing involves making a POST call like so: const data = querystring.stringify({ 'email& ...

Upon employing the setTimeout method, the element with the id "sign-in-block" will have its display style set to "none" after a delay of 1000 milliseconds

I incorporated the setTimeout() function to make the HTML component appear for 1 second upon pageload. However, I would prefer this component not to load at all. Should I set the delay in the setTimeout() function to 0ms? Alternatively, is there another ...

Code written in JavaScript inside an iframe

Is it possible to run JavaScript code inside an iframe even if the script with id "us" is loaded after creating the iframe? If so, how can this be accomplished? <iframe id="preview"> #document <html> < ...

Steps for deactivating tab stops on the primary webpage in the presence of a snackbar

On my web page, there are multiple drop-down menus, input fields, and buttons that can be interacted with using both the tab key and mouse. After entering certain data, I trigger a snackbar (source: https://www.w3schools.com/howto/howto_js_snackbar.asp) t ...

What is the best way to set up a simple example using a Node Stream.Readable?

I am currently exploring the concept of streams and encountering some difficulties in making it work properly. For this scenario, my goal is to send a static object to the stream and then pipe it to the server's response. This code snippet shows my ...

Are the elements in the second array the result of squaring each element in the first array? (CODEWARS)

I am currently working on a 6kyu challenge on codewars and I've encountered a frustrating issue that I can't seem to figure out. The task at hand involves comparing two arrays, a and b, to determine if they have the same elements with the same mu ...

Prompt user to leave page without saving changes on JQuery Dialog

I'm facing an issue with my jquery code that is supposed to display a dialog pop-up when a user tries to leave the page without saving changes. The dialog pops up as expected, but the page still proceeds to load. My goal is for the user to click a but ...

"Need a hand with bookmarklets? Try using this code: document

Having an issue with my bookmarklet JavaScript where it always adds the www. portion to the domain. Any ideas on how to make it work by removing the www portion from the URL? Appreciate any help. javascript:void(window.open('http://siteanalytics.comp ...

A guide on implementing the href attribute in Material-ui

I previously asked about this code, but my wording was unclear. I am trying to get the menu items to work with href links. Currently, only the "Home" button works with href, while the "Sessions Home", "Book a Session", and "[S] Host a session" buttons do n ...

Dealing with aliasing issues in shading effects using ShaderMaterial in three.js

Here is an image depicting my issue: as you can see, the shadows on my ShaderMaterial are not smooth. The material itself is a replica of PhongMaterial. https://i.sstatic.net/AzoYb.png Despite trying various solutions found online, I have been unable to ...