How to use node-modules installed by npm from template

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

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?

  1. import * as THREE from "/three";
  2. class App{
  3. constructor(){
  4. // 场景
  5. const scene = new THREE.Scene();
  6. // 相机
  7. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  8. // 渲染器
  9. const renderer = new THREE.WebGLRenderer();
  10. renderer.setSize(window.innerWidth, window.innerHeight);
  11. document.body.appendChild(renderer.domElement);
  12. renderer.render(scene, camera);
  13. }
  14. }
  15. window.onload = function(){
  16. new App();
  17. }

错误信息

import * as THREE from "/three";

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

import * as THREE from "three";

  1. 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";

  1. 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?

  1. import * as THREE from "/three";
  2. class App{
  3. constructor(){
  4. // 장면
  5. const scene = new THREE.Scene();
  6. // 카메라
  7. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  8. // 렌더러
  9. const renderer = new THREE.WebGLRenderer();
  10. renderer.setSize(window.innerWidth, window.innerHeight);
  11. document.body.appendChild(renderer.domElement);
  12. renderer.render(scene, camera);
  13. }
  14. }
  15. window.onload = function(){
  16. new App();
  17. }

error

import * as THREE from "/three";

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

import * as THREE from "three";

  1. 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";

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

答案1

得分: 1

以下是翻译好的内容:

  1. <script type="importmap">
  2. {
  3. "imports": {
  4. "three": "/node/three/build/three.module.js"
  5. }
  6. }
  7. </script>
  8. 通过修正作业的路径,问题已解决。
英文:
  1. &lt;script type=&quot;importmap&quot;&gt;
  2. {
  3. &quot;imports&quot;: {
  4. &quot;three&quot;: &quot;/node/three/build/three.module.js&quot;
  5. }
  6. }
  7. &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:

确定