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

查看:115
本文介绍了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. 使用<script>标记.
  2. 使用 CommonJS 实现.它具有Node.js之类的同步依赖项
  3. 使用异步模块定义(AMD)实现.
  1. Use the <script> tag.
  2. Use a CommonJS implementation. It has synchronous dependencies like Node.js
  3. Use an asynchronous module definition (AMD) implementation.

CommonJS 客户端实现包括(大多数在部署之前需要构建步骤):

CommonJS client side-implementations include (most of them require a build step before you deploy):

  1. 浏览器-您可以在浏览器中使用大多数Node.js模块.这是我个人的最爱.
  2. Webpack -完成所有操作(捆绑JavaScript代码,CSS等).它因React的兴起而广受欢迎,但因其学习曲线困难而臭名昭著.
  3. 汇总-一个新的竞争者.它利用ES6模块,并具有树状摇动功能(删除未使用的代码).
  1. Browserify - You can use most Node.js modules in the browser. This is my personal favorite.
  2. Webpack - Does everything (bundles JavaScript code, CSS, etc.). It was made popular by the surge of React, but it is notorious for its difficult learning curve.
  3. Rollup - a new contender. It leverages ES6 modules and includes tree-shaking abilities (removes unused code).

您可以阅读有关我的的比较的更多信息Browserify与(不推荐使用)组件.

AMD 的实现包括:

  1. RequireJS -在客户端JavaScript开发人员中非常受欢迎.由于它的异步特性,这不是我的口味.
  1. RequireJS - Very popular amongst client-side JavaScript developers. It is not my taste because of its asynchronous nature.

请注意,在选择要搭配的产品时,您会读到 Bower . Bower仅用于程序包依赖项,并且在CommonJS和AMD等模块定义中不受质疑.

Note, in your search for choosing which one to go with, you'll read about Bower. Bower is only for package dependencies and is unopinionated on module definitions like CommonJS and AMD.

这篇关于Node.js上的客户端:未捕获的ReferenceError:需求未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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