Receiving an unhandled error of type when importing OrbitControl.js

I've been experimenting with Javascript to incorporate a glb 3d model into my webpage and so far, everything is going smoothly.

However, once I try to import the OrbitControl.js (whether from my local directory or online), the browser throws me this error:

Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".*

Below is the complete code snippet:

import * as THREE from './three.js-master/three.js-master/build/three.module.js'
import {GLTFLoader} from './three.js-master/three.js-master/examples/jsm/loaders/GLTFLoader.js'
import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js"


const canvas = document.querySelector('.webgl')
const scene =  new THREE.Scene()

const loader = new GLTFLoader()
loader.load('assets/Prova.glb', function(glb){
    const root = glb.scene;
    root.scale.set(1,1,1)
    scene.add(root);
},function(xhr){
   console.log((xhr.loaded/xhr.total * 100) + "% loaded")
}, function(error){
    console.log('An error occured loading gltf')
})

const light = new THREE.DirectionalLight(0xffffff, 1)
light.position.set(2,2,5)
scene.add(light)


const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

const camera = new THREE.PerspectiveCamera(75, sizes.width/sizes.height, 0.1, 100)
camera.position.set(0,1,2)
scene.add(camera)




const renderer = new THREE.WebGL1Renderer

({
    canvas: canvas
})

renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
renderer.shadowMap.enabled = true
renderer.outputEncoding = THREE.sRGBEncoding

controls = new OrbitControls(camera, canvas)

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

animate()

Answer №1

Have you had a chance to take a look at the code in OrbitControls.js? It seems to be importing various components from 'three'.

import {
    EventDispatcher,
    MOUSE,
    Quaternion,
    Spherical,
    TOUCH,
    Vector2,
    Vector3
} from 'three';

This indicates that these components are being imported from 'three', so to address any issues, you may need to change the 'three' reference to match the location of your 'three.module.js' file. The original OrbitControls.js file was designed for npm installation.

Answer №2

To include the OrbitControls module in your project, you will need to import it from the examples directory, similar to how you imported the GLTFLoader.

Here is an example of how to do this:

import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

If you are using a path like three.js-master:

import { OrbitControls } from './three.js-master/three.js-master/examples/jsm/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

Unable to retrieve selected value with AJAX

My select box is set up with some values, and I'm using AJAX to send data to PHP and display that data inside a <div>. However, my code doesn't seem to be working properly. Interestingly, when I used a button to get the value from the sele ...

How to make a textarea that dynamically adjusts size based on amount of text inputted (using pure JavaScript, without jQuery)

As a server-side developer, I am venturing into the world of vanilla JS to expand my skillset. My current challenge involves creating a form where textareas are initially hidden behind button clicks and resize as more text is entered. This task is pushing ...

Why isn't my jQuery second click toggle functioning properly?

http://jsfiddle.net/motocomdigital/uTV5k/18/ I have recently made changes to use toggle instead of click, but I am still facing difficulties with smooth transitions. This code includes a combination of javascript and jquery. My goal is to create an anim ...

Ways to reveal concealed div elements when hovering the mouse?

Is there a way to display a group of hidden div's when hovering over them? For instance: <div id="div1">Div 1 Content</div> <div id="div2">Div 2 Content</div> <div id="div3">Div 3 Content</div> All div's sho ...

Recreating elements in ng-repeat using ng-click conditionally

I am attempting to swap an anchor tag with an image when clicked by the user. The code snippet I'm using looks like this: <div ng-repeat="postpart in currentPost.parts "> <div ng-if = "!postpart.isclicked"> <img ng-src= ...

Executing a Parent Page JavaScript Method from Within an iFrame

I have included an iFrame in my application. To illustrate, consider the following example: The main page contains <html> <head> <script src='jquery.js'></script> <script> function TestFunction() ...

The scroll() function in jQuery does not bubble up

Recently, I encountered an issue with a Wordpress plugin that displays popups on scroll. The code I currently have is as follows: jQuery(window).scroll(function(){ //display popup }); The problem arises with a particular website that has specific CSS ...

Sum the MongoDB aggregation until a certain condition is met (using if/else) or solve the problem by counting the price and quantity

THE ISSUE I am currently developing a function that calculates the average price and total value based on the requested quantity. For instance: async calculateValues (first_100) { let market_data = await auctions_db.aggregate([ { ...

The process of updating the value of an element in local storage with JavaScript

Most of the time we have an object stored in our localStorage. let car = { brand: "Tesla", model: "Model S" }; localStorage.setItem("car", JSON.stringify(car)); https://i.sstatic.net/qWEkE.png I am now eager to update the value of "model". H ...

Creating clones of <li> elements using JQuery and appending them to the end of a <ul> list

I have a regular ul li list and I am looking to add an additional li based on the first one in the list. Here is the HTML: <div id="gallery"> <ul> <li>Point Nr 1<p>lol</p></li> <li>Point Nr 2</li> <li> ...

Dynamic views loaded from ui-router can cause compatibility issues with running Javascript

Currently, I am facing an issue while attempting to load a template into a UI-View using UI-Router. Although the JavaScript is loaded, it does not run on the loaded views. The situation unfolds as follows: I have developed a template in HTML containing all ...

Tips for storing information in a database when a user decides to exit

Is there a proper method to achieve this task? I aim to store in the database the number of minutes that a user has watched in a video until they close the page, and then resume the video from where they left off when they revisit the same video. I attempt ...

Duplicate the identical data retrieval query, this time utilizing a JavaScript Ajax post method

To enhance the data request query, implement a JavaScript Ajax post method to avoid page refresh when the button is pressed. This will allow for requesting another page and displaying it in a designated section on the webpage. Here's the code snippet ...

The Vue DevTools are functioning as expected, but there seems to be an issue

Encountering a peculiar issue where the value displayed in Vue DevTools is accurate, matching what is expected in my data. When I first click on the "Edit" button for an item, the correct value appears in the browser window as intended. However, upon clic ...

What is the best way to generate a complete PDF of a webpage using pdfmake?

I'm currently developing a web application and facing the task of converting an HTML page, which contains multiple tables and datatables, to a PDF format. To achieve this, I've chosen to utilize the library pdfmake. Below is the script that I ha ...

By submitting a single request through $.when application

I am working on implementing $.when apply in my code. I have encountered an issue with the different return formats for single and multiple requests. How can this be handled without having to use another if else statement? $.when.apply(null, apiRequestLis ...

The data variable is typically implemented as __ob__ when it is being returned

Currently, I am working on creating a line chart using vue-chart.js. Here is the Vue component I am utilizing: Vue.component("line-plot", { extends: VueChartJs.Line, props: ["label", "data", "options"], mounted(){ ...

The error "TypeError: ollama.chat is not a function" has occurred when trying to use the ollama module in

Currently, I am grappling with a Node.js project that requires me to utilize the ollama module (ollama-js). The problem arises when I invoke the async function chatWithLlama() which contains ollama.chat(), resulting in the following error being thrown: Ty ...

Is there a JavaScript alternative to the .Contains method in C#?

I am currently working in C# where I have created an array of vowels to compare whether a string contains any elements from the array. However, I am unsure how to achieve this functionality in JavaScript. In C#, I utilize the .Contains() method to check if ...

Is it possible to eliminate the dedupe feature in npm?

By mistake, I accidentally ran the command npm dedupe and now all of my node_modules directories are flattened. While this reduces file size, it's making it more difficult to find things. Is there a way to reverse this and return to the hierarchical f ...