最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - THREE.js does not provide an export named EventDispatcher while loading OrbitControls in main.js - Stack Overflow

programmeradmin4浏览0评论

Description

I am trying to use the OrbitControls from THREE.js in my project. When I attempt to import the OrbitControls object, I am getting the following error message in the Google Chrome DevTools console.

The requested module './three.js' does not provide an export named 'EventDispatcher'

When I manually inspect three.js (r119), I can see that it does export EventDispatcher on line 50631.

Question: Given this information, why am I getting the aforementioned error message, and how can I fix this?

HTML

<!DOCTYPE html>
<html lang="en">
<body>
    <script src="three.js" crossorigin="anonymous"></script>
    <script src="main.js" type="module"></script>
</body>
</html>

main.js

import { OrbitControls } from './OrbitControls.js'

EDIT: Thanks to Steve, the root cause of my issue was that I was using the non-module version of three.js instead of the correct one (for my use case) called three.module.js. If you're getting the error message, make sure you're downloading the three.module.js file.

Description

I am trying to use the OrbitControls from THREE.js in my project. When I attempt to import the OrbitControls object, I am getting the following error message in the Google Chrome DevTools console.

The requested module './three.js' does not provide an export named 'EventDispatcher'

When I manually inspect three.js (r119), I can see that it does export EventDispatcher on line 50631.

Question: Given this information, why am I getting the aforementioned error message, and how can I fix this?

HTML

<!DOCTYPE html>
<html lang="en">
<body>
    <script src="three.js" crossorigin="anonymous"></script>
    <script src="main.js" type="module"></script>
</body>
</html>

main.js

import { OrbitControls } from './OrbitControls.js'

EDIT: Thanks to Steve, the root cause of my issue was that I was using the non-module version of three.js instead of the correct one (for my use case) called three.module.js. If you're getting the error message, make sure you're downloading the three.module.js file.

Share Improve this question edited Aug 5, 2020 at 17:02 asked Aug 5, 2020 at 16:18 user189198user189198 4
  • 1 OrbitControls are not included in 'three' instead you need to import them from examples, something like this: 'three/examples/jsm/controls/OrbitControls – George Commented Aug 5, 2020 at 16:25
  • @George Yeah, I should have mentioned that I already downloaded OrbitControls.js and included it in my project. It's that script that's throwing the error message. It's weird, because three.js clearly exports EventDispatcher, but OrbitControls.js doesn't seem to pick up on it – user189198 Commented Aug 5, 2020 at 16:33
  • Also, just FYI I'm not using a bundler. I literally just have an HTML page and a few JavaScript files locally. – user189198 Commented Aug 5, 2020 at 16:35
  • Then just import as i stated above instead of downloading it directly. it's probably trying to import something from three relatively but can't because it's no longer in the examples folder. See Orbit controls first lines import { EventDispatcher, MOUSE, Quaternion, Spherical, TOUCH, Vector2, Vector3 } from "../../../build/three.module.js"; – George Commented Aug 5, 2020 at 16:40
Add a ment  | 

1 Answer 1

Reset to default 6

I'm assuming you're using the OrbitControls.js found in https://github./mrdoob/three.js/blob/dev/examples/jsm/controls/OrbitControls.js

You're mixing the module library with the non-module library. If you look at the first lines of OrbitControls.js

import {
    EventDispatcher,
    MOUSE,
    Quaternion,
    Spherical,
    TOUCH,
    Vector2,
    Vector3
} from "../../../build/three.module.js";

It's trying to import variables from ../../../build/three.module.js, which probably doesn't exist. From your error message, it seems like someone already edited it to be './three.js', which sounds like the non-module version.

Solutions

Preserving references

  • Download all of three.js to a folder (like lib/three)
  • (Optionally) Delete files you don't need

Import via:

import { OrbitControls } from './lib/three/examples/jsm/controls/OrbitControls.js';
import * as THREE from './lib/three/build/three.module.js';

Modifying only the files you need

Or you can download only the files you need and change the references:

  • Download three.module.js from https://github./mrdoob/three.js/blob/dev/build/three.module.js
  • Replace ../../../build/three.module.js in OrbitControls.js to the appropriate path. It should be where three.module.js is in relationship to OrbitControls.js
  • Delete the <script src="three.js"> tag in your HTML, since OrbitControls.js directly imports it

Linking to a CDN

Alternatively, you can link directly to a CDN, like:

import { OrbitControls } from 'https://unpkg./[email protected]/examples/jsm/controls/OrbitControls.js';
import * as THREE from 'https://unpkg./[email protected]/build/three.module.js';

This works because all of the required files are hosted by the CDN

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论