Node.js 上的客户端:未捕获的 ReferenceError:未定义要求 [英] Client on Node.js: Uncaught ReferenceError: require is not defined

查看:20
本文介绍了Node.js 上的客户端:未捕获的 ReferenceError:未定义要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js、Express.js 和 Jade 编写应用程序 组合.

I am writing an application with the Node.js, Express.js, and Jade combination.

我有文件 client.js,它被加载到客户端.在那个文件中,我有从其他 JavaScript 文件调用函数的代码.我的尝试是使用

I have file client.js, which is loaded on the client. In that file I have code that calls functions from other JavaScript files. My attempt was to use

var m = require('./messages');

为了加载 messages.js 的内容(就像我在服务器端所做的那样),然后从该文件调用函数.但是,require 并没有在客户端定义,它会抛出 Uncaught ReferenceError: require is not defined 形式的错误.

in order to load the contents of messages.js (just like I do on the server side) and later on call functions from that file. However, require is not defined on the client side, and it throws an error of the form Uncaught ReferenceError: require is not defined.

这些其他 JavaScript 文件也在客户端运行时加载,因为我将链接放在网页的标题处.所以客户端知道从这些其他文件中导出的所有函数.

These other JavaScript files are also loaded at runtime at the client, because I place the links at the header of the webpage. So the client knows all the functions that are exported from these other files.

如何从打开到服务器的套接字的主 client.js 文件中的这些其他 JavaScript 文件(例如 messages.js)调用这些函数?

How do I call these functions from these other JavaScript files (such as messages.js) in the main client.js file that opens the socket to the server?

推荐答案

这是因为 require() 在浏览器/客户端 JavaScript 中不存在.

This is because require() does not exist in the browser/client-side JavaScript.

现在您将不得不对客户端 JavaScript 脚本管理做出一些选择.

Now you're going to have to make some choices about your client-side JavaScript script management.

您有三个选择:

  1. 使用
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆