2013-03-12 212 views
11

看來,這個簡單的應用程序不能找到'connect'模塊後,我剛剛安裝在文件目錄。錯誤:無法找到模塊'連接'

var connect = require ('connect'); 

connect.createServer(function(res, req, next) { 
    res.simpleBody("Connect you son of a beeeeetch"); 

}).listen(8000); 



[email protected] node_modules/express 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] ([email protected]) 
└── [email protected] ([email protected], [email protected], [email protected], [email protected]) 
Dzs-Mac-Attax-2:PlsWrk Dz$ node test.js 

module.js:340 
    throw err; 
    ^
Error: Cannot find module 'connect' 
    at Function.Module._resolveFilename (module.js:338:15) 
    at Function.Module._load (module.js:280:25) 
    at Module.require (module.js:362:17) 
    at require (module.js:378:17) 
    at Object.<anonymous> (/Users/Dz/Documents/NodeTests/PlsWrk/test.js:1:77) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.runMain (module.js:492:10) 

回答

18

Express內部使用Connect作爲其node_modules依賴項之一。

如果您希望在您的應用中使用Connect,則需要將其添加到您的package.json並運行npm update

按設計,每個節點模塊都有其自己的私有內部依賴關係。

如果您想使用任何應用程序,您的應用程序也需要安裝它們。

這意味着您的應用程序可以使用不同版本的模塊,並且每個模塊可以在內部再次使用不同版本而不會發生衝突。

+0

啊哈,我陷入了困境。謝謝。 – Architek1 2013-03-12 02:24:08

+0

它在package.json中添加explcit依賴項後運行,並在實際下載連接包到本地時運行npm update。 感謝您的提示 – rahul 2014-01-29 01:26:36

+0

如果您使用Express,它建立在Connect之上,那麼您應該使用Express來啓動服務器。如果您直接使用Connect,那麼Express用於什麼目的?第二臺服務器? – 2014-01-29 01:30:32

1

因爲您沒有安裝它。您已安裝express,而不是connect

+0

是不是將[email protected]模塊安裝在上面列出的Express中,還是必須單獨安裝? – Architek1 2013-03-12 02:09:09

+2

是的。但是除非你自己明確地安裝它,否則你不能'require()'。它是'express'的依賴,而不是你自己的。 – 2013-03-12 02:10:02

8

連接是Express使用的可擴展的HTTP服務器框架。特別是Express使用它提供對會話和cookie處理的支持。源代碼可在github上的https://github.com/senchalabs/connect上找到。

通常,當一個節點應用程序'找不到'的東西時,首先要嘗試的是去​​並搜索找不到的東西。如果不使用npm,在節點生態系統中很難相處。

在這種情況下,npmsearch會找到連接,如果你去https://npmjs.org/package/connect,你會發現連接模塊的安裝說明。

在這種情況下:

npm install connect 

npm -g install connect 

安裝用於每個用戶的連接模塊。

您可能需要成爲root用戶或使用sudo在大多數unix發行版上執行此操作。

另外,您可以添加連接依賴於你的項目的package.json文件 - 礦山看起來像這樣:

"express": "3.x" , 
"connect": "2.x" 

我也不得不使用NPM安裝的模塊緩衝區CRC32,方法,調試,新鮮,範圍解析器,cookie簽名和cookie來獲得工作快速項目。

快樂節點黑客:)

+0

謝謝爲我工作 – 2016-07-27 17:34:06

6

連接模塊或者未安裝或安裝部分所以,首先安裝它,後來就嘗試啓動服務器再次

npm install connect 
0

我有同樣的問題。節點安裝到目錄C:\Program Files (x86)\nodejs。最初我試圖從這個目錄安裝並運行JavaScript文件,這導致了許多權限問題。

如果您已將服務器置於該目錄中,請嘗試將其移動到另一個目錄(可能位於Documents),在Node命令提示符處導航並嘗試再次運行。

這對我來說是開箱即用的。

0

(Win7的),我作爲管理員 我做在C的目錄中打開命令窗口:\程序文件(x86)\的NodeJS 我導航通過CD C命令窗口:\程序文件(x86)\的NodeJS

我跑命令 NPM安裝連接

響應: [絕密]> NPM -g安裝連接 NPM ERR! Windows_NT 6.1.7601 npm ERR! argv「[redacted] \ node_modules \ npm \ bin \ npm-cli.js」「-g」「install」「connect」 npm ERR!節點v0.12.7 npm ERR! npm v2.11.3 npm ERR!代碼ETIMEDOUT npm ERR! errno ETIMEDOUT npm ERR!系統調用連接

npm ERR! network connect ETIMEDOUT 
npm ERR! network This is most likely not a problem with npm itself 
npm ERR! network and is related to network connectivity. 
npm ERR! network In most cases you are behind a proxy or have bad network settings. 
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the 
npm ERR! network 'proxy' config is set properly. See: 'npm help config' 

npm ERR! Please include the following file with any support request: 
npm ERR!  [redacted]\npm-debug.log 

我跑 NPM配置編輯 我改變了屬性行,以消除在每個這些行的開始時的分號。

同樣,我跑 NPM -g安裝連接

相同的結果。日誌文件有這個:

0 info it worked if it ends with ok 
1 verbose cli [ ' [redacted]\\\\node.exe', 
1 verbose cli ' [redacted]\\node_modules\\npm\\bin\\npm-cli.js', 
1 verbose cli '-g', 
1 verbose cli 'install', 
1 verbose cli 'connect' ] 
2 info using [email protected] 
3 info using [email protected] 
4 verbose install initial load of [redacted]\package.json 
5 verbose readDependencies loading dependencies from [redacted]\package.json 
6 silly cache add args [ 'connect', null ] 
7 verbose cache add spec connect 
8 silly cache add parsed spec { raw: 'connect', 
8 silly cache add scope: null, 
8 silly cache add name: 'connect', 
8 silly cache add rawSpec: '', 
8 silly cache add spec: '*', 
8 silly cache add type: 'range' } 
9 silly addNamed [email protected]* 
10 verbose addNamed "*" is a valid semver range for connect 
11 silly addNameRange { name: 'connect', range: '*', hasData: false } 
12 silly mapToRegistry name connect 
13 silly mapToRegistry using default registry 
14 silly mapToRegistry registry ht tps://registry.npmjs.org/ 
15 silly mapToRegistry uri ht tps://registry.npmjs.org/connect 
16 verbose addNameRange registry:ht tps://registry.npmjs.org/connect not in flight; fetching 
17 verbose request uri https://registry.npmjs.org/connect 
18 verbose request no auth needed 
19 info attempt registry request try #1 at 12:36:52 PM 
20 verbose request id 3524fc7ca91c73dc 
21 http request GET https://registry.npmjs.org/connect 
22 info retry will retry, error on last attempt: Error: connect ETIMEDOUT 
23 info attempt registry request try #2 at 12:37:26 PM 
24 http request GET https://registry.npmjs.org/connect 
25 info retry will retry, error on last attempt: Error: connect ETIMEDOUT 
26 info attempt registry request try #3 at 12:38:50 PM 
27 http request GET https://registry.npmjs.org/connect 
28 verbose stack Error: connect ETIMEDOUT 
28 verbose stack  at exports._errnoException (util.js:746:11) 
28 verbose stack  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) 
29 verbose cwd [redacted] 
30 error Windows_NT 6.1.7601 
31 error argv "[redacted]\\node_modules\\npm\\bin\\npm-cli.js" "-g"  "install" "connect" 
32 error node v0.12.7 
33 error npm v2.11.3 
34 error code ETIMEDOUT 
35 error errno ETIMEDOUT 
36 error syscall connect 
37 error network connect ETIMEDOUT 
37 error network This is most likely not a problem with npm itself 
37 error network and is related to network connectivity. 
37 error network In most cases you are behind a proxy or have bad network settings. 
37 error network 
37 error network If you are behind a proxy, please make sure that the 
37 error network 'proxy' config is set properly. See: 'npm help config' 
38 verbose exit [ 1, true ] 

我想我只會使用IIS Express。這數百文件做的事簡單就是超越我。

0

我對express-http-proxy有同樣的問題,解決方案是使用npm安裝軟件包。起初我全球安裝(-g),它的工作原理。在另一個安裝中,我不得不使用「--save-dev」。

$ gulp 
module.js:338 
    throw err; 
    ^
Error: Cannot find module 'express-http-proxy' 
at Function.Module._resolveFilename (module.js:336:15) 
at Function.Module._load (module.js:278:25) 
at Module.require (module.js:365:17) 
at require (module.js:384:17) 
at Object.<anonymous> (/var/opt/jet/workspace/node-js-project/lib/authorization-to-context-header.js:1:75) 
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) 

安裝包

$ npm install express-http-proxy -g 
[email protected] /opt/jet/nodejs/node-js/lib/node_modules/express-http-proxy 
âââ [email protected] ([email protected], [email protected]) 
âââ [email protected] ([email protected], [email protected]) 

,或者如果 「-g」 是遠遠不夠的

$ npm install express-http-proxy --save-dev 

一飲而盡工作

$ gulp 
[16:49:17] Using gulpfile /var/opt/workspace/node-js-project/gulpfile.js 
[16:49:17] Starting 'dev'... 
[16:49:17] Starting 'clean'... 
[16:49:17] Finished 'dev' after 9.18 ms 
[16:49:17] Starting 'default'... 
[16:49:17] Finished 'default' after 10 μs 
[16:49:17] Finished 'clean' after 11 ms 
[16:49:17] Starting 'build'... 
[16:49:17] Starting 'clean'... 
[16:49:17] Finished 'clean' after 2.13 ms 
[16:49:17] Starting 'styles'... 
[16:49:17] Finished 'styles' after 813 ms 
...