For a project made with react-three-fiber I imported the following libraries:
import * as THREE from 'three';
import React, { Suspense, useState } from "react";
import { Canvas, useLoader } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
which worked fine, then I decided to make a tidier UI for the scene and tried to import import { GUI } from '/jsm/libs/dat.gui.module'; according to https://sbcode.net/threejs/dat-gui/
However it showed an error:
Failed to compile
./src/App.js
Module not found: You attempted to import /jsm/libs/dat.gui.module which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
This error occurred during the build time and cannot be dismissed.
Which is odd because the four previous libraries are outside src.
I then tried putting the relative path but the compiler cannot resolve the path instead.
Then I tried moving the dat.gui.module.js file inside the src folder but the same Can't resolve error appeared.
This is the folder structure of my project:
-Project
|-node_modules
| L-(all the ext. libs including @react-three etc.)
|-src
| L-App.js
L-public
L-index.html
How do I get dat.gui working in my react-three-fiber project?
You have to import it like
import { GUI } from 'three/examples/jsm/libs/dat.gui.module'. Then you can use it insideuseEffectlike below example.