How should I fix the error message "TypeError encountered while attempting to import OrbitControls"?

Encountering error while attempting to import OrbitControls

JS:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});
const cube = new THREE.Mesh(geometry, material);
const controls = new OrbitControls(camera, renderer.domElement);

renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

scene.add(cube);

camera.position.set(0, 20, 100);
camera.position.z = 5;

renderer.render(scene, camera);

controls.update();

function animate(){
    requestAnimationFrame(animate);

    controls.update();

    renderer.render(scene, camera);
}

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Document</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js" ></script>
        <script type="module" src="task9.js"></script>
    </body>
</html>

Attempted using alternatives: import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; and import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js'; However, none of them were successful

Answer №1

Consider using this method instead. Your code appears to have a few issues that need addressing…

  1. Avoid duplicating camera settings.
  2. Make sure to initialize OrbitControls after the renderer.
  3. Include some lights in the scene.
  4. Eliminate any unnecessary controls.update();
  5. Ensure to call the animation loop.

Check out this revised version:

import * as THREE from "https://esm.sh/three";
import { OrbitControls } from "https://esm.sh/three/examples/jsm/controls/OrbitControls";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});
const cube = new THREE.Mesh(geometry, material);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

scene.add(cube);

camera.position.z = 5;
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enabled = true;

function animate() {
    requestAnimationFrame(animate);
    controls.update();
    renderer.render(scene, camera);
}
animate();

Also, take a look at the following resources:

Three.js breaks when trying to import OrbitControls.js

Answer №2

import OrbitControls from '../three/examples/jsm/controls/OrbitControls'

Give this a shot as it could potentially resolve the issue you're facing.

Answer №3

If you encounter an error message stating that the path to OrbitControls must be absolute rather than relative, it simply means that you need to specify the full path to OrbitControls. You can learn more about the distinctions. To resolve this, replace the path to OrbitControls with either './three/examples/jsm/controls/OrbitControls' or '/home/&USER/path/to/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

Issue with Angular: Unable to update model before modal closure on submit

I have a search form that is displayed within a modal window created using Angular UI Bootstrap. The input fields in the form update the ng-model when submitted. <script type="text/ng-template" id="mobileSearchPanel"> <form> ...

Calculating the sha1 hash of large files using JavaScript in the browser without causing excessive memory usage

Is there a method to calculate the hash of large files in JavaScript without needing to load the entire file in a FileReader? Specifically, I'm interested in finding out if it's possible to stream a file and calculate its sha1 hash in JavaScript. ...

Encountering a Material UI error: Incorrect hook usage when combining create-react-library with MUI

After transitioning from Material Ui v3 to v4 on a create-react-library project, I encountered an issue. This particular project serves as a dependency for other projects in order to share components. However, when attempting to display a material-ui compo ...

Unable to synchronize Rijdnael encryption across C# and Javascript/Node platforms

I have encountered an issue while trying to convert a Rijndael encryption function from C# to Node. Despite using the same Key, IV, Mode, and Block Size, I am unable to achieve matching results. What could be causing this discrepancy? C# MRE: using System ...

Running AngularJS controllers should only occur once the initialization process has been fully completed

I am facing a situation where I need to load some essential global data before any controller is triggered in my AngularJS application. This essentially means resolving dependencies on a global level within AngularJS. As an example, let's consider a ...

Tips for building an API using an app router in Next.js

After setting up my new project and using the app router as recommended in the latest version of Next.js, I am now looking to create an API. How can I go about creating and utilizing this API within my page app/user/page.tsx? I have already created an API ...

Making React function properly on GitHub Pages

I have followed all the steps and even consulted multiple tutorials that all say the same thing, but when I try to run "npm run deploy" I encounter an error and nothing happens. This is the error message: [email protected] deploy A:\Documents&bs ...

What is the best way to arrange <div> elements with text inside without moving the text?

I need help figuring out how to stack one or more <div> elements on top of another <div> element while maintaining the text content within each. My issue is illustrated in this fiddle: https://jsfiddle.net/rd268ucy/1/. When attempting to drag o ...

Redux - a method of updating state through actions

Hello, I am currently working on developing a lottery system and I have a question regarding the best approach to modify state using action payloads. Let's consider the initial state: type initialCartState = { productsFromPreviousSession: Product[ ...

What are the steps to retrieve all memcached information using node.js?

One of my main objectives is to have the user session data expire when they close their browser. However, I am facing a challenge because my Server depends on memcached to function correctly. Therefore, I need to find a way to specifically delete the use ...

Unable to transfer AJAX data to PHP script

Trying to figure out how to send AJAX data to PHP. While I am comfortable with PHP, JavaScript is a bit new to me. Incorporating HTML / JavaScript <input type="text" id="commodity_code"><button id="button"> = </button> <script id="s ...

Real-time filtering in personalized dropdown menu with search input

I have been attempting to create a custom list filter for input using a computed property. In one file, I am developing a widget, while in another file, I am implementing it. Below is the code snippet from the widget creation file: <template> ...

Receiving the latest global variable in JavaScript using jQuery

I keep receiving undefined type for: $(".ui-slider-handle").attr("title", '"'+current+'"'); Even though I can successfully alert the updated value of current on line 29, it seems to not be working with .at ...

Is it possible to extract all parameters, including nested or within arrays, from a Swagger File using Node.js?

Looking for a method to extract and interpret data such as parameters from a Swagger file. Specifically, I am working with the Petstore Swagger API ( ). The definitions within the file contain references to other components, like parameters. One example ...

When modifying the state of an array within a component, certain values may be overwritten and lost in the process

Currently, I'm faced with the challenge of ensuring that a component displays a loading screen until all images have completed loading. This is necessary because there are approximately 25 images that must finish loading before the page can be display ...

Using querySelector to Target Elements by their InnerHTML Content

Is it possible to target an element by its innerHTML directly without the use of loops? Is there a way to achieve this with something like document.querySelector('div[innerHTML="Sometext"]') or document.querySelector('div[textcontent="Som ...

How to manage ajax URLs across multiple pages?

I have my website set up at http://example.com/foo/ within a directory, separate from the main domain. Through the use of .htaccess, I've configured the URLs to appear as http://example.com/foo/about/, http://example.com/foo/polls/, http://example.com ...

I am unable to input just one numerical value into my function

I am encountering an issue with my function that calls another function. The problem arises when inputting single numbers in the prompt; I have to append a letter like (a1, a2, a3) for it to function correctly. The function "PrintSelectedToPDF" works smoo ...

When I shift a JavaScript function from inline code to a separate JS file, I encounter the error message "function is not defined"

I am struggling to get a simple task done: <!DOCTYPE html> <html lang="en"> <head> </head> <body> <script src="js/test.js" type="text/js"></script> <button onclick="test()" type="submit">Test</button> ...

What is the functionality of the filterBy method in Vue.js 1.x?

Looking to create a filter for my app similar to the filterBY Vue.js feature on older versions... Currently working on a Computed Property to generate an array containing objects that match a specified string variable... However, my goal is to create a re ...