Module import error in THREE.js

Currently, I am attempting to bring in the threejs and GLTFLoader modules into my project. Both of these modules (just for testing purposes) are located within the same root/js/ directory.

import * as THREE from './js/build/three.module.js'; // Importing THREE module works without any issues 
import { GLTFLoader } from './js/build/GLTFLoader.js'; // Attempting to import GLTFLoader throws a MIME TYPE error

I understand that there is a mimetype problem, however, it's strange that this error does not occur when using the standard three master 'structure'. Can anyone explain why this doesn't work?

EDIT:

Upon uncommenting the GLTF import line, the following error message is displayed:

Loading module from “http://localhost/dev/project/build/three.module.js” 
was blocked because of a disallowed MIME type (“text/html”).

The issue seems to be related to the path of three.module.js file, but surprisingly everything loads correctly with no errors when this line is commented out. Rest assured, all paths for files and folders are accurate.

Answer №1

My initial question may have been unclear, but essentially I was looking to leverage the modular functionality of three.js while keeping my entire project within a single root folder, avoiding the need to navigate up to the 'build' files.

This is a common requirement, especially when not using webpack or similar tools, as most websites are typically contained within a single root folder.

With some helpful advice, I was able to address this issue as follows:

Folder Structure:

root/
 |
 | -- index.html
 |
 |
 | -- /build/
 |      |
 |      | -- three.module.js 
 |
 |
 | -- /js/
        |
        | -- app.js*
        |
        | -- /jsm/
               |
               | -- /libs/ ...        
               | -- /loaders/ ...

Imports (in app.js):

import * as THREE from '../build/three.module.js';
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import { TWEEN } from './jsm/libs/tween.module.min.js';
import Stats from './jsm/libs/stats.module.js';

I hope this solution is helpful for anyone facing a similar challenge, and I would like to express my gratitude to the community for their assistance!

Answer №2

This specific line of code needs to be adjusted:

import { GLTFLoader } from './js/build/GLTFLoader.js';

You should change it to:

import { GLTFLoader } from './js/examples/jsm/loaders/GLTFLoader.js';

Furthermore, ensure that your organization of three.js files aligns with the official three.js folder structure.

For more details, refer to this helpful answer.

I want to emphasize that modifying the GLTFLoader.js file, as suggested by @ScieCode, is not advisable. It would necessitate editing multiple other files such as OrbitControls.js, TrackballControls.js, and EffectComposer.js whenever you update versions. By adhering to the recommended folder structure outlined in the provided link, you can avoid unnecessary edits and complications.

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

Incorporate a class into the fixed navigation menu using fullpage.js

I am attempting to modify the behavior of a sticky menu as the user scrolls down using fullpage.js. My goal is to have the #top-wrapper behave normally when the first section/page loads, and then add a class of is-fixed as you scroll down. When scrolling ...

Image not showing up when using drawImage() from canvas rendering context 2D

Need help with drawImage() method in JavaScript <head> </head> <body> <script type = "text/javascript"> var body, canvas, img, cxt; body = document.getElementsByTagName("body" ...

On the second attempt to call setState within the componentDidMount method, it is not functioning as expected

As a newcomer, I am delving into the creation of a memory game. The main objective is to fetch data from an API and filter it to only include items with image links. On level one of the game, the task is to display three random images from the fetched data ...

Managing data in Vuex by updating it from a child component using $emit

I'm currently working on a Vue app that has the capability to randomize a title and subtitle or allow users to manually edit these values using a custom input component. The challenge I'm facing is updating the parent component and state to refle ...

Hover Effect with CSS Selector - JavaScript or jQuery Implementation

I have a similar code snippet: <article class="leading-0"> <h2> <a href="link">link title</a> </h2> <ul class="actions"> <li class="print-icon"> <a ...><img...>& ...

Exploring the Possibilities of OpenLayers with Scalable Vector

Trying to create a webpage with an image that can be navigated using drag and scroll events, similar to Google Maps. Instead of building it from scratch, I attempted to achieve this using OpenLayers, with the intention of using the image in place of a map. ...

Having issues with the right margin in HTML5 canvas and struggling to remove the scroll bar

I am struggling with adjusting the margins of my canvas to ensure equal spacing on both sides without any horizontal scroll bars. Desired Outcome: Equal margin on both sides of canvas without any horizontal scroll bars. Issue: The margin-right property i ...

Axios mistakenly sending DELETE requests before GET requests

I'm currently developing a Vue.js project that is connected to a Lumen API and everything is functioning smoothly. Within this project, I have implemented a list of students ('Etudiants') that allows users to select and delete a student by ...

Create specification for the properties of the child component

I am interested in working with the props of a parent element's children and validating those props. Can I achieve this using prop-types? export default function MyComponent(props) { return ( <div> {React.Children.map(props.chil ...

What is the best way to ensure a navigation link stops scrolling at the bottom of a navbar that is set to position: sticky?

I am utilizing Bootstrap 5 for my website, and I have a sticky-top attribute on my navbar. When I click on one of the links in the navigation bar, the page scrolls to that section, but some of the content goes behind the navbar. I am looking for a way to m ...

Prevent JavaScript from sending a POST request to a specific URL

Currently facing Cross Site Scripting (XSS) vulnerabilities in a web application, I am curious if there are security measures equivalent to Content-Security-Policy: frame-ancestors and X-Frame-Options for JavaScript. My objective is to restrict the abilit ...

Would you suggest using angularjs for authentication in large-scale applications?

As I continue to learn and utilize the AngularJS framework, I have noticed that while some of its features are impressive, some key aspects make it challenging for authentication-based applications. For example, let's consider a scenario where a webs ...

JavaScript: Receiving an error that function is undefined when working with data binding

Why is my code showing that it's not defined while I'm attempting a simple code with data binding? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht ...

The synchronization and network bandwidth optimization of Angular and Firebase's three-way binding system

Is it possible to synchronize the text box content across various clients using Angular and Firebase? I am curious to know if Firebase will update the database with each letter I type. How does this process impact network bandwidth? Can you explain how it ...

Currently, I am working on developing a Discord bot using discord.js within Visual Studio Code. So far, all of the commands that I have implemented are functioning properly except for one. This specific command is a

Currently, I am immersed in the development of a comprehensive AIO Discord Bot similar to popular ones like "Dyno Bot" or "Carl Bot." The initial phase involved creating basic commands such as ping, avatar, and so on. Now, I have ventured into crafting a ...

What is the process for including an optional ngModelGroup in Angular forms?

I'm encountering an issue with incorporating the optional ngModelGroup in angular forms. Although I am familiar with how to use ngModelGroup in angular forms, I am struggling to figure out a way to make it optional. I have attempted to pass false, nu ...

Can SailsJS be used exclusively for API processes?

Can SailsJS be used solely as an API? After downloading the Sails project, is it possible to exclude the views and focus only on utilizing Sails as an API? ...

Identify all paths with or without the .html suffix, excluding static resources

I am working on configuring a universal handler for requests that do not pertain to images or favicons. I want this handler to manage paths like /index, /index.html, /user/123, and similar, while excluding /favicon.ico, /sunflower.png, /images/starfish.png ...

How can I use Java script to find specific text within table rows on a website?

I am looking to create a dynamic table that filters rows based on user input in an input text box. Here is an example table: Row 1: Apples Row 2: Oranges Row 3: Bananas When a user starts typing in the text box, I want the rows to filter accordingly. ...

Implement a smooth transition effect when changing the image source

I am currently working on enhancing a Squarespace website by adding a new image fade-in/fade-out effect when the mouse hovers over one of three buttons. The challenge I'm facing is that the main static image is embedded as an img element in the HTML r ...