未定义需求函数

问题描述 投票:0回答:1

我正在使用Express开发3D查看器。

我的问题是,我试图在我的JS文件中的一个(不是主要的)文件中需要文件系统模块。当我尝试加载浏览器时,控制台会显示下一条消息:

ReferenceError:未定义require

我的项目结构是:

  • node_module
  • src
    • js
      • app.js //问题在这里
    • 意见
      • index.ejs
  • server.js
javascript require referenceerror
1个回答
0
投票

关于如何在浏览器中使用导入的简短示例:

  1. 使用下一个内容创建index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src="./main.js"></script>
</body>
</html>

检查脚本标签-我们设置type =“ module”-这指示浏览器我们正在使用模块系统

  1. 使用以下方法创建main.js脚本:
import name from './name.js';

console.log(name);
  1. 创建name.js文件:
const name = 'test';
export default name;

现在您需要运行此应用。最简单的方法是使用WebServer For Chrome

启动应用程序并检查控制台。您应该看到已记录“测试”。

就是这样。请记住,这只是一个例子。我们不涉及缩小,缓存和其他重要事项。

© www.soinside.com 2019 - 2024. All rights reserved.