2
我試圖設置一個小型的node.js
webserver,它將使用jQuery
和jQuery-ui
。我使用Express
來設置一個小項目,然後使用npm <package-name> install --save
安裝我需要的所有軟件包。請記住,我是JavaScript新手,所以我可能會做一些愚蠢的事情,但我找不到任何人有相同的問題(只是相關)。「TypeError:undefined不是函數」當需要jQuery-ui在node.js中使用Express
Express
已成立項目是這樣的:
$ 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
只是我現在還包括jQuery
和jQuery-ui
,像這樣:
運行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, e.g., $.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的原因嗎?我沒有看到它以前用過這種方式,並且會猜測它使用'npm'和'require'來爲browserify或其他CommonJS到瀏覽器環境提供服務。 –
那麼,我想創建一個小型網頁來訪問和更改服務器上的數據庫。這應該包括一些用戶界面,下拉菜單,選項卡等來插入和刪除數據。這不是設置事物的正確方法嗎?再次,我是這個世界的新手,所以這個設置可能不可行。 –
在這種情況下,您應該將jQuery和jQuery-ui作爲靜態文件提供,並使用'
jQuery和jQuery的UI是爲了在前端使用,並且預計不會沒有DOM與節點的工作。如果您想使用任何助手jQuery函數,您可以使用包含類似功能的underscore.js。
如果你想創建一些HTML界面以便你的快遞服務器返回,那麼這兩個庫(jQuery和jQuery-UI)應該在模板文件(jade文件)中定義/包含(使用標籤)。
希望這有助於
來源
2015-04-23 10:05:21