简体   繁体   English

无法解析模块说明符 thre

[英]Failed to resolve module specifier thre

I am trying to work on the Three.js and now this time in three modules.我正在尝试在 Three.js 上工作,这次是在三个模块中。 I am using express as a backend server.我使用 express 作为后端服务器。

I am constantly getting error of:我不断收到以下错误:

Uncaught TypeError: Failed to resolve module specifier "three".未捕获的类型错误:无法解析模块说明符“三”。 Relative references must start with either "/", "./", or "../".相对引用必须以“/”、“./”或“../”开头。 in my console.在我的控制台中。

This is the backend code of app.js这是 app.js 的后端代码

 "use strict";

 const express = require("express");
 const app = express();
 const path = require("path");

 const port = process.env.PORT || 9000;

 app.use(express.static(__dirname + "/public"));

 app.use("/build/", express.static(path.join(__dirname, "node_modules/three/build")));
 app.use("/jsm/", express.static(path.join(__dirname, "node_modules/three/examples/jsm")));

 app.get("/", (req, res) => {
 response.send("Connected successfully");
});

app.listen(port, (error) => {
 if (error) {
  console.warn(`${error} occured while starting server`);
  return;
 }
 console.log(`listening successfully to the port ${port}`);
});

and this is my js file:这是我的 js 文件:

import * as THREE from "/build/three.module.js";
import { OrbitControls } from "/jsm/controls/OrbitControls.js";
import Stats from "/jsm/libs/stats.module.js";

let scene, camera, renderer;

function main() {
 scene = new THREE.Scene();

 const windowsWidth = window.innerWidth;
 const windowsHeight = window.innerHeight;

 renderer = new THREE.WebGLRenderer({ antialias: true });
 renderer.setSize(windowsWidth, windowsHeight);
 document.body.appendChild(renderer.domElement);

 const aspect = windowsWidth / windowsHeight;
 camera = new THREE.PerspectiveCamera(90, aspect, 0.1, 1000);
 camera.position.z = 2;
 // camera.position.set(-900, -200, -900);

 const controls = new OrbitControls(camera, renderer.domElement);
 controls.addEventListener("change", renderer);

 let materialArrays = [];

 let textureImages = [
   "meadow_ft.jpg",
   "meadow_bk.jpg",
   "meadow_up.jpg",
   "meadow_dn.jpg",
   "meadow_rt.jpg",
   "meadow_lf.jpg",
 ];

 for (let i = 0; i < 6; i++) {
 let texture = new THREE.TextureLoader().load(
   `../images/skyboxday1/greenery/${textureImages[i]}`
 );
 let material = new THREE.MeshBasicMaterial({ map: texture });
 materialArrays.push(material);
}

const geometry = new THREE.boxGeometry(10000, 10000, 10000);

let skyBox = new THREE.Mesh(geometry, materialArrays);
scene.add(skyBox);
function draw() {
  renderer.render(scene, camera);
  requestAnimationFrame(draw);
  }
 }

main();

I included my js file in HTML with:我在 HTML 中包含了我的 js 文件:

<script type="module" src="./client.js"></script>

This is my folder structure:这是我的文件夹结构:

在此处输入图像描述

You need to provide "importmap" to the browser, in index.html add script type importmap before您需要向浏览器提供“importmap”,在 index.html 之前添加脚本类型 importmap

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>deore.in</title>    
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            overflow: hidden;
            margin: 0px;
        }
    </style>
    <script type="importmap">
        {
            "imports": {
                "three": "./build/three.module.js",
                "three/examples/jsm/controls/OrbitControls":"./jsm/controls/OrbitControls.js",
                "three/examples/jsm/libs/stats.module":"./jsm/libs/stats.module.js",
            }
        }
    </script>
</head>

<body>
    <script type="module" src="client.js"></script>
</body>

</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 导入 package 无法解析模块说明符 - Javascript import package Failed to resolve module specifier 如何解决 npm package 中的“无法解析模块说明符”错误? - How to resolve "Failed to resolve module specifier" error in a npm package? 在 Webpack 中使用 &#39;externals&#39; 会出现错误“无法解析模块说明符” - Using 'externals' in Webpack gives error "failed to resolve module specifier" 无法解析模块说明符“sortablejs”。 相对引用必须以“/”、“./”或“../”开头 - Failed to resolve module specifier "sortablejs". Relative references must start with either "/", "./", or "../" 在 ES6 中导入包:“无法解析模块说明符“vue”” - importing a package in ES6: "Failed to resolve module specifier "vue"" “不能在模块外使用导入语句”后跟“未捕获的类型错误:无法解析模块说明符” - 'Cannot use import statement outside a module' followed by 'Uncaught TypeError: Failed to resolve module specifier' 无法解析模块说明符“vee-validate”。 相对引用必须以“/”、“./”或“../”开头 - Failed to resolve module specifier "vee-validate". Relative references must start with either "/", "./", or "../" 我的 React 18 的 Webpack 配置显示 Uncaught TypeError: Failed to resolve module specifier &quot;react-dom/client&quot; - My Webpack configuration for React 18 shows Uncaught TypeError: Failed to resolve module specifier "react-dom/client" 未使用 node.js:未捕获类型错误:无法解析模块说明符“三”。 相对引用必须以“/”、“./”或“../”开头 - Not using node.js : Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../" 无法解析模块说明符“express”。 我在浏览器中收到此错误 - Failed to resolve module specifier "express". I am getting this error in my browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM