首页 > 解决方案 > Javascript 给我 Uncaught ReferenceError: require is not defined 错误

问题描述

我有一个看起来像这样的文件结构: 在此处输入图像描述

我的 index.js 有这个代码

const THREE = require("three")

var scene = new THREE.scene();
console.log(scene)

我的 index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="webgl-output">What the hell</div>
  <script src="./index.js"></script>
</body>
</html>

我没有在 package.json 中使用标签 { type : "modules }。但是,我仍然收到 Uncaught ReferenceError: require is not defined 错误。我可以知道是什么原因吗?谢谢!

我的实时服务器中的错误看起来像这样 在此处输入图像描述

如果您有兴趣,这是我的 package.json

{
  "name": "threejs-playground",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "three": "^0.133.1"
  }
}

标签: javascripthtmlthree.js

解决方案


推荐阅读