首页 > 文章列表 > 通过 npm 如何从模板中安装 Node.js 模块

通过 npm 如何从模板中安装 Node.js 模块

473 2024-02-05
问题内容

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

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

之后,我尝试导入并使用 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();
}

错误

从“/三”导入*作为三;

http://localhost:8081/three net::err_aborted 404 (not found)

从“三”导入*作为三;

uncaught typeerror: failed to resolve module specifier "three". relative references must start with either "/", "./", or "../".

main.go 中的 e.static("/node", "node_modules")

从“/node/node_modules/三/build/third.module.js”导入*为三;

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


正确答案


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

通过更正作业路径已解决该问题。