What could be causing the issue with importing the OBJLoader module in Three.js?

I've been struggling with an issue for a few days now. I can't figure out how to import OBJLoader for Three.js. This is crucial for incorporating custom 3D models into my game.


I'm seeing two error messages:

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

and

Failed to load resource: the server responded with a status of 404 (Not Found)


import {OBJLoader} from 'three-obj-loader'

    // load a resource
    loader.load(

        // resource URL
        '/Users/dmitri/Desktop/flame/models/fresh_fire_01 (1).obj',
        // called when resource is loaded
        function ( object ) {

            scene.add( object );
        },

        // called when loading is in progresses
        function ( xhr ) {

            console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },

        // called when loading has errors
        function ( error ) {

            console.log( 'An error happened' );
        }
    );

    // const loader = new OBJLoader()
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000)
    const renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(0xffffff)

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

    // loader.load("/Users/dmitri/Desktop/flame/models/fresh_fire_01(1).obj",
    // function(object){
    //     scene.add(object)
    // })

    var run = function() {
        requestAnimationFrame(run);
        renderer.render(scene, camera)
    }
    run()

And here is the HTML code:

<html>
        <head>
            <style>
                body{
                    margin: 0;
                    padding: 0;
                }
            </style>
        </head>

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

            <script src="/script.js" type="module"></script>
        </body>

        </html>

Here's my app.js source code:

const express = require("express");
        var app = express();

        app.set("port", process.env.PORT || 8080)

        app.use(express.static("public"))

        app.listen(app.get("port"), function() {
            return
        })

Here's my file structure:


models

--fresh-fire-01 (1).obj

public

--index.html

--script.js

app.py


I have exhausted all possible solutions. I placed the OBJLoader source in my directory, required it, and even inserted the OBJLoader source code inside my script.js file. Unfortunately, nothing seemed to work.

Answer №1

When using Three.js, it is recommended to utilize the native OBJLoader instead of third-party alternatives. The 'three-obj-loader' may appear outdated with its last update being 4 years ago. To stay current, import OBJLoader directly from the three.js library:

Change:

import {OBJLoader} from 'three-obj-loader'

to:

import {OBJLoader} from 'three/examples/jsm/loaders/OBJLoader.js'

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

Receiving JSON in a C# web service: A step-by-step guide

I created a JSON string using jQuery and now I want to send it to a C# web API controller. Here is an example of the JSON object: {"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]} I attempted to send it with a URL structure like thi ...

DirectUpload is failing to trigger directUploadWillStoreFileWithXHR for file storage

I have implemented Rails ActiveStorage on an ECS class import { DirectUpload } from "@rails/activestorage"; function createDirectUpload(file, source, controller) { return new DirectUpload(file, source.url, source.token, source.attachmentName, ...

How come my variable becomes 0 even though I assigned it a value that is not 0?

I initially set out to calculate the distance between the top of the page and the visible area that the viewer is currently at, in terms of pixels (vertically). The goal was for a function to scroll down on another page based on how far the user had scroll ...

Adjust the classes of the static navigation bar based on the background of the section it is positioned over

In the process of developing a website using Bootstrap 4, I encountered a challenge with sections featuring both light and dark backgrounds along with a fixed navbar. The navbar, which is set to dark using the css class bg-dark, becomes indistinguishable ...

I am looking for a way to add some color to the text within a div on my HTML page. Can you help me

Is there a way to apply background color only to the text content within this div, without styling the entire element? ...

Execute the validation directive using the digest cycle

This particular directive is designed to determine whether a given value exists within the associated datalist or not. It functions flawlessly when I input text into the field, but it fails to work properly if the datalist undergoes changes as a result o ...

What is the best way to use html, css, jquery, and bootstrap to show or hide images without any extra space or alignment issues

I am looking to showcase 6 images on a page using the Bootstrap grid layout. The images should be displayed in two rows, with 3 images in each row. Additionally, I want to implement a filter functionality using Isotope, so users can click on a specific cat ...

Need to obtain the stack trace from the catch block in a request-p

Currently, I am utilizing the 'request-promise' library in my node-js application for making API calls. However, I am facing challenges in obtaining the correct call stack from the 'catch' function. Upon experimenting with it, I discove ...

Unable to bring JavaScript into an HTML document

I am diving into the fundamentals of JS and HTML, starting with a simple script. Here is my test.js file: document.getElementById("test").innerHTML = "Loaded js file"; And here is my test.html: <!DOCTYPE HTML> <html lang="de"> <head> & ...

When trying to run a jQuery function on click or load events, an error message stating that

When I have an .on(click) event triggering an ajax call, I want the same actions to occur when the page loads as well. My idea is to create a function that contains everything within the .on(click) event and trigger this function on page load. Although I ...

`Even though I specified `{cache: $templateCache}` in the $http call, the directive is still showing $templateCache as

I have two HTML pages named snippet1.html and snippet2.html. I want to use them within my directive in order to add multiple templates using a single directive. To achieve this, I initially tried adding the HTML templates within <script> tags and as ...

Show/Hide a row in a table with a text input based on the selected dropdown choice using Javascript

Can someone please assist me with this issue? When I choose Business/Corporate from the dropdown menu, the table row becomes visible as expected. However, when I switch back to Residential/Consumer, the row does not hide. My goal is to only display the row ...

How to Retrieve Content from an iFrame Using jQuery?

I am facing a challenge with an HTML form that is directed to an IFRAME upon clicking "submit". The iframe then loads a page after the submission, which indicates whether the user-input data is valid or not. It simply displays on screen as "TRUE" or "FALSE ...

Is the AJAX function failing to execute? Potential issue with the database connection?

I am currently working on setting up my project on the localhost. I've encountered an issue with the built-in ajax function upon success. Below is the snippet of the javascript function in question: function fetchResults(page, searchString) { page ...

Sending a JS function to a PHP script

I've been incorporating the Soundcloud API into my project and successfully implemented their record button on my website. Once the upload is completed, the code generates the URL of the newly created soundcloud file. My goal now is to pass that URL ...

Mastering the art of jQuery UI development, similar to the techniques

I'm currently working on creating an abstraction layer for jQuery UI that enables the definition of Widgets as Objects, similar to ExtJS. Here's how it works: var mydialog = new $.ui.dialog({ modal:true, renderTo:'body', title:'T ...

Is there a way to generate a SVG path connecting various DIV elements programmatically?

How can I achieve a dynamic SVG path between my word DIVs? This is my current progress and this is my desired outcome. Could you please explain why my code isn't working as expected? I attempted to calculate the XY positions of my words to create an ...

What is the best way to add 1 to the number in my array?

I'm currently learning JavaScript and I have a function that should increment my count by 1 every time it's called. However, when I run it in the console, it just keeps repeating 1. Does anyone have any suggestions? var output = []; var count = ...

What is the process for sending a JSON response and then redirecting to an HTML page after a successful event in Node.js using Express?

Trying to send a JSON response and redirect the page simultaneously in Express.js. Need help figuring out how to achieve this. Is it possible to redirect in Express.js while sending a JSON response to the client? The goal is to use this JSON data to render ...

Trouble parsing a long JSON string with JSON.parse and angular.fromJson

I am attempting to replace the angularjs POST request with a standalone JSON response string. Previously, the angular GET / POST requests would automatically convert the response to JSON which worked perfectly. Now, I am trying to store the JSON response ...