Encountered an issue resolving the module specifier 'three' - The seamless integration of Three.js with Wordpress has unexpectedly stopped working

I am the administrator of a WordPress website that features a captivating Three.js animation on its homepage. I created this site several months ago and everything was functioning perfectly - I even demonstrated it to someone just a week ago and encountered no issues. However, when I checked the site yesterday, I discovered that Three.js was failing to load, and I received the following error message in the console:

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

Interestingly, there is no specific line or file mentioned in the error log, as it seems to originate from the root domain's first line. It is important to note that I had not made any changes to the setup whatsoever. I had not even upgraded to WP 5.9. This is exactly how I configured it initially. Although it may not adhere to best practices, I want to stress that it was working flawlessly at one point.

Including the animation javascript file and setting its type to "module":

function my_custom_scripts() {
    wp_enqueue_script( 'gsap', 'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.7.0/gsap.min.js', array( 'jquery' ), '', true);
    wp_enqueue_script( 'scrolltrigger', 'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.7.0/ScrollTrigger.min.js', array( 'gsap' ), '', true);
    wp_enqueue_script( 'custom-animations', get_stylesheet_directory_uri() . '/js/animations.js', array( 'gsap' ),'',true );
    
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts', 15);

add_filter('script_loader_tag','add_type_to_script', 10, 3);
function add_type_to_script($tag, $handle, $source){
    if ('custom-animations' === $handle) {
        $tag = '<script type="module" src="'. $source .'"></script>';
    } 
    return $tag;
}

The initial lines of the "animations.js" file:

import * as THREE from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c58445e49496c1c021d1d11604a464b4b"></a>[email protected]/build/three.module.js';
import { GLTFLoader } from "https://threejs.org/examples/jsm/loaders/GLTFLoader.js";
import { DRACOLoader } from 'https://threejs.org/examples/jsm/loaders/DRACOLoader.js';

If there are other relevant code snippets needed to address this issue, please inform me. I have extensively reviewed the situation and attempted various solutions, including A) downloading the module file onto my own server and specifying a relative path, but the same console error persists (although a 404 error occurs if the link is invalid, indicating that it was followed). B) downloading the three.min.js file and loading it before the current one, eliminating the module call entirely, yet the console error remains (despite it not being imported as a module). The site is hosted on Cloudflare, so I disabled it initially. Clearing the cache did not change anything, and I verified at each step that a cached version was not being loaded. In Chrome dev tools' Network tab, the module/file is successfully loaded into the cache. At some point along the process, a particular file fails to attach "Three" as a module and the reason eludes me. Any assistance provided would be greatly appreciated!

Answer №1

Encountered an identical issue recently. I had shared my project on Github a month ago () and had not made any changes since then. However, approximately a week back, an unexpected error began appearing in the console. Upon investigation, I discovered the cause:

<script type="importmap">
    {
        "imports": {
            "three": "../master/three.js-master/build/three.module.js"
        }
    }
</script>

To resolve this, the import statement for three.js in scripts.js needed to be adjusted as follows:

import * as THREE from 'three'

By making these modifications and referencing the updated code on Github, the issue was successfully resolved.

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

How can I adjust a number using a shifter in JavaScript?

Searching for an event handler where I can use a shifter to adjust the value of a number by moving it left or right. Would appreciate any links to documentation on how to achieve this. Many thanks UPDATE Thanks to the suggestions from other users, I hav ...

Is it possible to save edits made to CKEDITOR by clicking a button located outside of the editor?

I am having an issue with inserting HTML code into my CKEDITOR. I have a button on the page that, when clicked, calls: editor.insertElement(link); After inserting the HTML code correctly into the editor, any changes made (such as clicking the 'show ...

Error while making an API call in AngularJS using the $http GET method

I'm currently in the process of developing a straightforward app using Angular that will interact with my API. To test the code, I'm utilizing a virtual machine and accessing it on my computer. When calling the API from my local machine, I can us ...

event handler in JavaScript that triggers upon a click

Encountering an issue with my code <tr> <td id="<?php echo; $id ?>" onclick="addrow(?php echo $id; ?>)">...</td> </tr> <tr id="<?php echo $id; ?>" class="ndisplay">...</tr> <tr id="<?php echo $i ...

Switch Background Image - The image failed to display

Here is the code snippet I am currently working with. It involves displaying a background-image when clicking on a link with the class 'home-link', and not showing an image if the link does not have this class. The issue at hand: The problem ari ...

How to work with multiple selections in React material-ui Autocomplete

I'm currently using Material-ui Autocomplete in multiple selection mode. My goal is to retrieve all the selected values when the form is submitted. Although I found a solution on how to get individual values through the onChange event handler from thi ...

Creating a CSS animation to mimic the fading in and out effect of the Mac scrollbar when scrolling begins

My journey begins with this: *::-webkit-scrollbar { } *::-webkit-scrollbar-button { } *::-webkit-scrollbar-track { } *::-webkit-scrollbar-track-piece { } *::-webkit-scrollbar-thumb:active { width: 6px; background-color: red; } *::-webkit-scr ...

Utilizing the OrientDB HTTP API within an Angular platform - a comprehensive guide

When trying to query OrientDB from an Angular service method, authentication-related errors are encountered. It appears that two GET requests are required for successful querying of OrientDB. An Authentication call: Requesting http://localhost:2480/conne ...

Angular displaying a blank screen, even though the complete dataset is available

I am currently working on my first website using Angular and I've encountered a problem. When I click on 'view project', it should return the data specific to that item. The strange thing is, when I log my JavaScript console, I can see all t ...

Retrieving localStorage data from another webpage

I recently created an account and I hope my question is clear. I have two separate pages on my website - one for a menu and the other for an HTML game. The menu has two buttons, one to start a new game and the other to continue from where you left off. How ...

Determining the position of a v-for element in Vue.js when making an HTTP request

I'm faced with a challenge involving an array of posts with comments, each post having an input for creating a new comment. The posts array contains the id of each post as a prop, but I'm unsure how to select the specific post for which I want to ...

Exploring the capabilities of arrays within Ajax

Below is the original code I wrote in JavaScript: var wt_val = []; for (i = 0; i<human_wt.length; i++){ var mult; mult = data_list[basket_list[button_port_name][i]].map(x => x*(wt[i]/100)); wt_val.push(mult); ...

Tips for retrieving the li value from localStorage

This is a snippet from my coding project. function getStudentList() { $.getJSON('http://mydomain.com/getStudents.php?jsoncallback=?', function(data) { $('#studentList li').remove(); $('#load').hide(); ...

Tips for managing and capturing errors in Express

const database = require('database'); const express = require('express'); const app = express(); const cors = require('cors'); app.use(cors()); const bodyParser = require('body-parser'); const urlencodedParser = body ...

Refreshing a Nextjs page upon clicking a route button for the very first time

Essentially, when the initial page is built and we are on the index page, clicking any button that changes the route - using methods like push or replace in the-router function triggers a page refresh. After the first reload, subsequent clicks on the but ...

processing a substantial document and utilizing the splitby technique

Currently, I am attempting to utilize the splitby method within highland.js framework to isolate the data situated between the specified begin and end delimiters. -----BEGIN DATA----- MIIEzDCCArSgAwIBAgIVCugKYzMN5ra8zPWxYE8pUU9SxjYSMA0GCSq ...

Filtering arrays in JavaScript results in an array without any elements

// Performing data merging test const Array_1 = [{ class_1: 4, HeadTeacher: "Mrs Joe" }]; const Array_2 = [{ class_1: 10, HeadTeacher: "Loveth" }]; const Array_3 = [{ class_1: 1, HeadTeacher: "Itunu" }]; const Array_4 = [{ class_1: 1, HeadTeacher: "John" ...

The method `collectionGroup` is not recognized as a function within the context of _firebase__WEBPACK_IMPORTED_MODULE_10__.usersCollection

Having an issue similar to the title. I am encountering an error while attempting to download my 'hives' using collectionGroup, and I'm unsure of how to resolve it. view image details here Below is the code snippet: async fetchHives() { ...

Update the CURLOPT_URL parameter to accept user input

As someone new to coding, I have a question about using the CURLOPT_URL function in PHP. How can I input a NIK number and retrieve data from this URL: ? Any help would be appreciated, thank you! php $curl = curl_init(); curl_setopt_array($curl, [ CU ...

Maintaining TextBox State in ASP.Net Through Postbacks

Can anyone help me figure out how to maintain the control state that has been modified in Javascript? I am working with two TextBoxes, one DropDownList, and a button (all Runat=Server) in C# ASP.net 2010 Express. The first textbox accepts any user in ...