How to use node-modules installed by npm from template

huangapple go评论74阅读模式
英文:

How to use node-modules installed by npm from template

问题

我正在尝试在Go模板中使用通过npm安装的库。

我通过'npm install three'安装了必要的三个库,保存在根文件夹中,如下面的截图所示。

How to use node-modules installed by npm from template

之后,我尝试导入和使用three.js模块,但是找不到threejs。

我认为文件系统存在一些问题,我该如何在我的情况下无问题地使用threejs?

import * as THREE from "/three";

class App{
    constructor(){
        // 场景
        const scene = new THREE.Scene();

        // 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

        // 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

        renderer.render(scene, camera);
    }
}

window.onload = function(){
    new App();
}

错误信息

import * as THREE from "/three";

http://localhost:8081/three net::ERR_ABORTED 404 (Not Found)

import * as THREE from "three";

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

在main.go中使用e.Static("/node", "node_modules")

import * as THREE from "/node/node_modules/three/builld/three.module.js";

GET http://localhost:8081/node/three/builld/three.module.js net::ERR_ABORTED 404 (Not Found)
英文:

I am trying to use a library installed by npm in the Go template.

I installed the necessary three libraries through 'npm install three', saved in the root folder as shown in the screenshot below.

How to use node-modules installed by npm from template

After that, I try to import and use the three.js module as shown below, but threejs is not found.

I think there is some problem with the filesystem, how can I use threejs without problems in my situation?

import * as THREE from "/three";

class App{
    constructor(){
        // 장면
        const scene = new THREE.Scene();

        // 카메라
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

        // 렌더러
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

        renderer.render(scene, camera);
    }
}

window.onload = function(){
    new App();
}

error

import * as THREE from "/three";

http://localhost:8081/three net::ERR_ABORTED 404 (Not Found)

import * as THREE from "three";

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

e.Static("/node", "node_modules") in main.go

import * as THREE from "/node/node_modules/three/builld/three.module.js";

GET http://localhost:8081/node/three/builld/three.module.js net::ERR_ABORTED 404 (Not Found)

答案1

得分: 1

以下是翻译好的内容:

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

通过修正作业的路径,问题已解决。
英文:
&lt;script type=&quot;importmap&quot;&gt;
    {
        &quot;imports&quot;: {
            &quot;three&quot;: &quot;/node/three/build/three.module.js&quot;
        }
    }
&lt;/script&gt;

It was resolved by correcting the path to the job.

huangapple
  • 本文由 发表于 2023年2月14日 22:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定