【问题标题】:Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/”未捕获的类型错误:解析模块说明符“三”时出错。相对模块说明符必须以“./”、“../”或“/”开头
【发布时间】:2022-08-19 09:32:54
【问题描述】:

我正在使用three.js制作水,但我不明白出了什么问题,给了我错误..

Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/”.

如下图所示。

我用来创建水的 HTML 代码是:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=\"utf-8\">
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
                <script type=\"importmap\">
        {
            \"imports\": {
                \"three\": \"./build/three.module.js\",
                \"three.js-master/examples/jsm/controls/OrbitControls\":\"./jsm/controls/OrbitControls.js\",
                \"three.js-master/examples/jsm/libs/stats.module\":\"./jsm/libs/stats.module.js\",
            }
        }
    </script>
    </head>
    <body>
        
        <script type=\"module\" src=\"https://cdn.jsdelivr.net/npm/three@0.143/build/three.min.js\"></script>
        <script type=\"module\" src=\"https://cdn.jsdelivr.net/npm/three@0.143/examples/js/controls/OrbitControls.js\"></script> 
        
        
        </script>
        <script  type=\"module\">

            import { Water } from \'http://localhost/Lines/jsm/objects/Water.js\';
            import * as THREE from \'http://localhost/Lines/build/three.module.js\';

            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
            camera.position.set( 0, 0, 50 );
            camera.lookAt( 0, 0, 0 );
            

            const renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );

        

            const WaterGeometry = new THREE.PlaneGeometry( 45, 45 );
            const texture = new THREE.TextureLoader().load( \'https://i.imgur.com/mK9cHLq.jpeg\' );
            const material = new THREE.MeshBasicMaterial( { map: texture, side: THREE.DoubleSide } );       
            const controls = new THREE.OrbitControls( camera, renderer.domElement );

            let water = new Water
                (
                    WaterGeometry,
                    {
                        textureWidth: 512,
                        textureHeight: 512,
                        waterNormals: new THREE.TextureLoader().load
                        ( \'textures/waternormals.jpg\', function ( texture ) 
                        
                            {

                             texture.wrapS = texture.wrapT = THREE.RepeatWrapping;

                            } 
                        ),
                        sunDirection: new THREE.Vector3(),
                        sunColor: 0xffffff,
                        waterColor: 0x001e0f,
                        distortionScale: 3.7,
                        fog: scene.fog !== undefined
                    }
                );



            controls.update();

            scene.add( water );



            function animate() 
            {
                requestAnimationFrame( animate );

                water.rotation.x += 0.00;
                water.rotation.y += 0.00;
                water.rotation.z += 0.01;

                renderer.render( scene, camera );
            };

            animate();
        </script>
    </body>
</html>

我不明白为什么我的代码会抛出该错误。很确定在我的文件夹中导入的 JavaScript 代码 Water.js 没有问题,而我的 HTML 代码就是问题所在。

我无法弄清楚出了什么问题,或者我可能根本不知道 Javascript 是如何工作的以及她的真实本性。

为什么我的代码会抛出这种错误。请赐教。

谢谢!

  • 这回答了你的问题了吗? Failed to resolve module specifier three
  • @Marquizzo我试图理解并复制您链接上的部分代码,但我仍然遇到同样的错误。我现在真的很沮丧,因为我不知道为什么我的代码会抛出那个错误。自从我遇到这种情况已经一周了,每天看到我的代码我都感到沮丧。我认为three.js 很酷,但似乎她很难使用....尤其是导入一些东西。该死的,为什么我对此不聪明..诅咒这个。我什至不知道如何以不会引发该错误的方式编写 HTML 代码。我用你在链接中所说的提示在答案中编写了代码。

标签: three.js


【解决方案1】:

我有同样的问题,它与 HTML 无关。对我来说,当我在网络服务器上上传脚本时出现了问题。我只需要更改导入路径(在需要的地方添加 ../../)。我希望这可以帮助你。

【讨论】:

    【解决方案2】:

    当我使用 Firefox 运行 html 文件时出现此错误。当我使用 Chrome 时,它​​运行良好。问题似乎是 Firefox 尚不支持导入映射,这是他们方面已知的bug。

    【讨论】:

      【解决方案3】:

      这是我用来抛出错误的代码:

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset="utf-8">
              <title>My first three.js app</title>
              <style>
                  body { margin: 0; }
              </style>
                      <script type="importmap">
              {
                  "imports": {
                      "three": "./build/three.module.js",
                      "three.js-master/examples/jsm/controls/OrbitControls":"./jsm/controls/OrbitControls.js",
                      "three.js-master/examples/jsm/libs/stats.module":"./jsm/libs/stats.module.js",
                  }
              }
          </script>
          </head>
          <body>
              
              <script type="module" src="https://cdn.jsdelivr.net/npm/three@0.143/build/three.min.js"></script>
              <script type="module" src="https://cdn.jsdelivr.net/npm/three@0.143/examples/js/controls/OrbitControls.js"></script> 
              
              
              </script>
              <script  type="module">
      
                  import { Water } from 'http://localhost/Lines/jsm/objects/Water.js';
                  import * as THREE from 'http://localhost/Lines/build/three.module.js';
      
                  const scene = new THREE.Scene();
                  const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
                  camera.position.set( 0, 0, 50 );
                  camera.lookAt( 0, 0, 0 );
                  
      
                  const renderer = new THREE.WebGLRenderer();
                  renderer.setSize( window.innerWidth, window.innerHeight );
                  document.body.appendChild( renderer.domElement );
      
              
      
                  const WaterGeometry = new THREE.PlaneGeometry( 45, 45 );
                  const texture = new THREE.TextureLoader().load( 'https://i.imgur.com/mK9cHLq.jpeg' );
                  const material = new THREE.MeshBasicMaterial( { map: texture, side: THREE.DoubleSide } );       
                  const controls = new THREE.OrbitControls( camera, renderer.domElement );
      
                  let water = new Water
                      (
                          WaterGeometry,
                          {
                              textureWidth: 512,
                              textureHeight: 512,
                              waterNormals: new THREE.TextureLoader().load
                              ( 'textures/waternormals.jpg', function ( texture ) 
                              
                                  {
      
                                   texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
      
                                  } 
                              ),
                              sunDirection: new THREE.Vector3(),
                              sunColor: 0xffffff,
                              waterColor: 0x001e0f,
                              distortionScale: 3.7,
                              fog: scene.fog !== undefined
                          }
                      );
      
      
      
                  controls.update();
      
                  scene.add( water );
      
      
      
                  function animate() 
                  {
                      requestAnimationFrame( animate );
      
                      water.rotation.x += 0.00;
                      water.rotation.y += 0.00;
                      water.rotation.z += 0.01;
      
                      renderer.render( scene, camera );
                  };
      
                  animate();
              </script>
          </body>
      </html>
      

      我不知道如何以不会引发上述错误的方式编写 HTML 代码。

      【讨论】:

      • 请编辑您的原始问题以添加其他信息。
      • @TheJim01 我编辑了来自评论者反馈的 HTML。该死的......我希望我能更聪明地理解这一点。我不再知道我在做什么。我羡慕你们懂这种陌生的语言和逻辑。我已经处于崩溃的边缘,不知道我的病的真正答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2020-01-17
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多