There was an error in Three.js: "Uncaught ReferenceError: THREE is not defined"

Every time I try to execute my javascript code, I encounter the error message "Uncaught ReferenceError: THREE is not defined". The problematic line in my code is:

var renderer = new THREE.WebGLRenderer();

// I have included the three.js library in the script tag so I'm stumped as to what the issue may be.

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set = (0, 0, 10);
camera.lookAt(camera.position);
scene.add(camera);

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0.0, 1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(1.0, -1.0, 0.0));
geometry.faces.push(new THREE.Face3(0, 1, 2));

var material = new THREE.BasicMeshMaterial({
    color: 0xFFFFFF,
    side: THREE.DoubleSide
});

var mesh = new THREE.Mesh(geometry, material);
mesh.position.set(-1.5, 0.0, 4.0);
scene.add(mesh);

function render() {
    renderer.render(scene, camera);
}

render();

Answer №1

To successfully execute this code, three.js must be included first

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>

Answer №2

When I faced a similar issue, I found that specifying the character encoding in the header resolved the problem for me

<head>
<title>Cube by Charden</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
.
  .
  .
  .
  
</head>

Answer №3

One error I made was referencing the three.js file from the /src/Three.js directory. It should actually be referenced from ./build/three.js instead.

Answer №4

It can be challenging to identify the error in your code as you have only provided the section where you are using Three.js. It is crucial to also consider how you are loading the module and the sequence in which all the initialization scripts are executed. I suggest sharing a simple example either through jsfiddle or utilizing the Code Snippet feature in your main post.

To ensure that three.min.js is properly loaded:

  • Check your browser's console for any errors.
  • Inspect the network section in your browser's console to verify if three.min.js is listed there.

index.html

<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
</head>
  <body>
  </body>
 </html>
<script>
  var container = document.createElement( 'div' );
  document.body.appendChild( container );
  scene = new THREE.Scene();
    ....
</script>

Answer №5

After encountering a similar issue, I discovered my mistake was placing

<script src="./libs/three.js"></script>
after the script that requires the use of three in my HTML document.

Answer №6

Ensure to accurately verify the directory of the three.js file, which resides in a folder in my particular scenario.

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

Is using an interval the best method to ensure that an Ajax call has finished successfully?

I have a scenario with two input text fields and a save button. <input type="text" id="myInput" /> <input type="text" id="searchResult"/> <button id="myButton>save</button> The myInput t ...

The challenge of populating information within a Datalist

In my code snippet, I have a JavaScript function that is used to populate a Datalist: function PopulateDropDown(facility) { $.ajax({ url: '/Rentals/Base/GetContactsForFacility?selectedFacility=' + facility, data: { facility: ...

What causes getBoundingClientRect() in Javascript to occasionally produce decimal values?

Currently, I am experimenting with an HTML5 canvas element. A major concern of mine is setting up a mousemove event to monitor the movement of the mouse over the canvas for drawing and other purposes. Unfortunately, I have not been able to locate a definit ...

Ways to verify if both the select and input fields are selected and populated with jQuery

In my html form, I have a select field and an input box with two classes being used: work_phone_class and work_phone_class_input. I am triggering ajax by clicking a save button with a specific class whenever the value in the select field is changed or any ...

Hey there everyone, I was wondering how to send both single and multiple values to a database using JavaScript and JSON endpoints with a Spring Web API

{ "id": 178, "stockin_date": "2022-11-15T08:18:54.252+00:00", "effective_date": null, "expired_date": null, "create_date": null, "update_date&q ...

The blur event is failing to trigger in my custom input component

// The Custom Input component const Box = styled.div``; const InputBox = styled.div<{ width: string }>` display: flex; width: ${(props) => props.width}; height: 42px; justify-content: space-between; align-items: center; padding: 9px ...

React: Issue with function not recognizing changes in global variable

When the run button is clicked, it triggers an event. However, clicking on the skip button does not take me to Switch Case 2 as expected. Even though the skip state updates, the function still outputs the old value of skip. const CustomComponent = () => ...

What are some ways to slow down the speed of my animation?

Currently, I am creating a HTML5 game where I have implemented javascript to make my character move in response to the user pressing the arrow keys. The movement animation consists of 6 sprites. However, I have encountered an issue where when I hold down ...

Guide to forming an array by extracting specific properties from a nested JSON array using javascript

Currently, I have this list: list = { id: 1, arr: [ {index : 1 , description: "lol" , author: "Arthur"}, {index : 2 , description: "sdadsa" , author: "Bob"}, {index : 3 , desc ...

In the scenario where there are duplicate 'id' keys within the array of objects, what is the best method to remove the object with the duplicate key?

When there are duplicate 'id' keys among the objects in the array, how can you remove the object with the duplicated 'id'? I attempted to use filter, map, and set methods, but none of them were successful. Since the array is not one-di ...

Error Encountered While Creating a Polygon Wallet on Fireblocks

After following the instructions from Fireblocks Docs, I successfully created a default wallet named "BTC_TEST" like this: enter image description here. However, when attempting to create a Matic wallet, I encountered an Axios Error. Despite Matic being a ...

What is the proper way to change this JavaScript variable into JSON format?

I have a JavaScript variable containing data that I need to convert into valid JSON format for easier parsing. The current variable structure is as follows: { "machines": [{ "category": "Category 1", "items": [{ "name": "Te ...

Bootstrap / Blazor - no action when clicking on an image link

Having issues with adding a simple HTML link around an image in my Bootstrap 4.5 / Blazor app. When I click on the link, nothing happens. How is this possible? I've tried using the <a href"..."><img ...> pattern, as well as NavL ...

Can a value be concealed within a dropdown list on a form (using PHP or JavaScript)?

Within my form, there is a drop down box listing the 50 states in the U.S. This list is generated using a PHP for loop. Users are required to select their residential state and later on, they must also choose a mailing state if it differs from their resi ...

Encountering an issue with executing Google API Geocode in JavaScript

I'm having trouble printing an address on the console log. Every time I run the code, I encounter an error message that reads: { "error_message" : "Invalid request. Missing the 'address', 'components', 'latlng' or &ap ...

Update the state both before and after executing the API call

I'm facing an issue with the setState function where it seems to be getting called again before completing the previous batch of state updates. My data object has the following structure: [{ id: 0, loading: false }] On my webpage, I have toggle butt ...

Tips for presenting styled HTML content within a dynamic list using HTML and JavaScript

Is there a way to display HTML formatted text within a dynamic list in HTML? I've tried implementing it, but the tags are being displayed instead of the formatted text. Here's an example of the code: <!DOCTYPE html> <html> <body> ...

Guide to making an `Ajax Login` button

I am interested in creating a SIGN IN button using ajax. Specifically, I want it to display something (such as welcome) on the same page without refreshing it. This is the progress I have made so far: update2: <form id="myForm" onsubmit="return signi ...

Organize the array following the guidelines of a card game with a versatile approach

deck = ['Jack', 8, 2, 6, 'King', 5, 3, 'Queen', "Jack", "Queen", "King"] <!- Desired Result = [2,3,5,6,8,'Jack','Queen','King'] Explore the challenge: Arrange the ...

Enable the script tag with the type "module" only under certain conditions

Attempting to dynamically enable script tags in HTML using the code below does not yield the expected results. function loadScript() { document.querySelectorAll('script[type="text/skip-hydration"]').forEach((script) => { script ...