当使用Express在node.js中要求使用jQuery-ui时,“TypeError:undefined不是一个函数”

我正在尝试设置一个小型的node.js webserver,它将使用jQueryjQuery-ui 。 我用Express来build立一个小项目,然后用npm <package-name> install --save我需要的所有包。 请记住,我是JavaScript新手,所以我可能会做一些愚蠢的事情,但我找不到任何人有相同的问题(只是相关的)。

Express已经build立了这样的项目:

 $ ls app.js bin node_modules npm-debug.log package.json public routes views 

文件夹node_modules现在包含:

 $ ls node_modules body-parser cookie-parser debug express jade jquery jquery-ui morgan serve-favicon 

我的app.js文件是由Express设置的,除了我现在包含jQueryjQuery-ui ,像这样:

 $ cat app.js var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var jQuery = require('jquery'); var jQueryUi = require('jquery-ui'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // The rest is omitted here for brevity. Default settings from Express. 

运行node bin/www现在显示错误

 /home/oystein/QAWebApp3/node_modules/jquery-ui/jquery-ui.js:15 $.extend( $.ui, { ^ TypeError: undefined is not a function at /home/oystein/QAWebApp3/node_modules/jquery-ui/jquery-ui.js:15:3 at Object.<anonymous> (/home/oystein/QAWebApp3/node_modules/jquery-ui/jquery-ui.js:316:3) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (/home/oystein/QAWebApp3/app.js:8:16) at Module._compile (module.js:460:26) 

这个错误似乎来源于jquery-ui.js文件,如下所示:

 var jQuery = require('jquery'); /*! jQuery UI - v1.10.3 - 2013-05-03 * http://jqueryui.com * Includes: jquery.ui.core.js, jquery.ui.widget.js, jquery.ui.mouse.js, jquery.ui.draggable.js, jquery.ui.droppable.js, jquery.ui.resizable.js, jquery.ui.selectable.js, jquery.ui.sortable.js, jquery.ui.effect.js, jquery.ui.accordion.js, jquery.ui.autocomplete.js, jquery.ui.button.js, jquery.ui.datepicker.js, jquery.ui.dialog.js, jquery.ui.effect-blind.js, jquery.ui.effect-bounce.js, jquery.ui.effect-clip.js, jquery.ui.effect-drop.js, jquery.ui.effect-explode.js, jquery.ui.effect-fade.js, jquery.ui.effect-fold.js, jquery.ui.effect-highlight.js, jquery.ui.effect-pulsate.js, jquery.ui.effect-scale.js, jquery.ui.effect-shake.js, jquery.ui.effect-slide.js, jquery.ui.effect-transfer.js, jquery.ui.menu.js, jquery.ui.position.js, jquery.ui.progressbar.js, jquery.ui.slider.js, jquery.ui.spinner.js, jquery.ui.tabs.js, jquery.ui.tooltip.js * Copyright 2013 jQuery Foundation and other contributors; Licensed MIT */ (function( $, undefined ) { var uuid = 0, runiqueId = /^ui-id-\d+$/; // $.ui might exist from components with no dependencies, eg, $.ui.position $.ui = $.ui || {}; $.extend( $.ui, { version: "1.10.3", keyCode: { BACKSPACE: 8, COMMA: 188, DELETE: 46, DOWN: 40, // Lots of other defines... })( jQuery ); // End of block on line 316 

我的猜测是一些包含问题。 错误在第15行, $.extend( $.ui, { ,但消息告诉我看看行(function( $, undefined ) { 。解决这个问题的最好方法是什么?

呵呵, package.json看起来像这样:

 { "name": "QAWebApp3", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "body-parser": "^1.12.3", "cookie-parser": "^1.3.4", "debug": "^2.1.3", "express": "^4.12.3", "jade": "^1.9.2", "jquery": "^2.1.3", "jquery-ui": "^1.10.5", "morgan": "^1.5.2", "serve-favicon": "^2.2.0" } } 

要在客户端上使用,jQuery和jQuery-UI文件应作为服务器上的静态文件提供,而Express有一个static中间件,您可以使用该中间件来定义目录以查找静态文件。

这些文件可以从jQuery站点下载,也可以像Bower一样用客户端软件包pipe理器进行pipe理,因为从npm看来,jQuery-UI使用的是浏览器不支持的require

你也可以使用托pipe的jQuery文件,所以你不需要在服务器上。

然后,一个<script>标签应该被添加到你的html(或Jade)文件来加载和使用它们。

jQuery和jQuery-UI意味着在前端使用,并且不需要在没有DOM的情况下使用Node。 如果您想使用任何助手jQuery函数,可以使用包含相似function的underscore.js 。

如果你想创build一些HTML界面以便你的快递服务器返回,那么这两个库(jQuery和jQuery-UI)应该在模板文件(jade文件)中定义/包含(使用标签)。

希望这可以帮助