错误:无法找到模块'连接'

问题描述:

看来,这个简单的应用程序不能找到'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) 

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

因为您没有安装它。您已安装express,而不是connect

+0

是不是将[email protected]模块安装在上面列出的Express中,还是必须单独安装? – Architek1 2013-03-12 02:09:09

+2

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

连接是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

连接模块或者未安装或安装部分所以,首先安装它,后来就尝试启动服务器再次

npm install connect 

我有同样的问题。节点安装到目录C:\Program Files (x86)\nodejs。最初我试图从这个目录安装并运行JavaScript文件,这导致了许多权限问题。

如果您已将服务器置于该目录中,请尝试将其移动到另一个目录(可能位于Documents),在Node命令提示符处导航并尝试再次运行。

这对我来说是开箱即用的。

(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。这数百文件做的事简单就是超越我。

我对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 
...