错误:找不到模块'connect'

时间:2013-03-12 01:59:26

标签: node.js connect

看来这个简单的应用程序在我刚安装到文件目录后找不到'connect'模块。

var connect = require ('connect');

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

}).listen(8000);



express@3.1.0 node_modules/express
├── methods@0.0.1
├── fresh@0.1.0
├── range-parser@0.0.4
├── cookie-signature@0.0.1
├── buffer-crc32@0.1.1
├── cookie@0.0.5
├── debug@0.7.2
├── commander@0.6.1
├── mkdirp@0.3.3
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.2 (pause@0.0.1, bytes@0.1.0, formidable@1.0.11, qs@0.5.1)
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)

8 个答案:

答案 0 :(得分:20)

Express在内部使用Connect作为其node_modules依赖项之一。

如果您希望在应用中使用Connect,则需要将其添加到package.json并运行npm update

按照设计,每个节点模块都有自己的私有内部依赖项。

如果您想使用任何,您的应用也需要安装它们。

这意味着您的应用可以使用不同版本的模块,每个模块可以在内部再次使用不同版本而不会发生冲突。

答案 1 :(得分:8)

Connect 是Express使用的可扩展HTTP服务器框架。特别是Express使用它为会话和cookie处理提供支持。源代码可在https://github.com/senchalabs/connect的github上获得。

通常,当节点应用程序“无法找到”时,首先要尝试的是转到https://npmjs.org/package/npm-search并搜索无法找到的内容。如果不使用npm,很难在节点生态系统中相处。

在这种情况下,npmsearch会找到connect,如果你转到https://npmjs.org/package/connect,你会找到连接模块的安装说明。

在这种情况下:

npm install connect

npm -g install connect 

为每个用户安装connect模块。

您可能需要root或使用sudo在大多数unix发行版上执行此操作。

或者,您可以将connect依赖项添加到项目的package.json文件中 - 我的样子如下:

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

我还必须使用npm来安装模块buffer-crc32,方法,调试,新鲜,范围解析器,cookie签名和cookie来获得一个有效的快速项目。

快乐节点黑客攻击:)

答案 2 :(得分:6)

连接模块未安装或部分安装,因此请先安装它,然后再尝试再次启动服务器

  npm install connect

答案 3 :(得分:1)

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

答案 4 :(得分:1)

您可以使用以下命令,如果您忘记安装connect serve-static模块或安装不正确,则可能会发生这种情况。

$ npm install connect serve-static

答案 5 :(得分:0)

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

如果您已将服务器放在该目录中,请尝试将其移至另一个目录(可能在Documents中),在Node命令提示符中导航,然后再次尝试运行。

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

答案 6 :(得分:0)

(Win7)我有一个以管理员身份打开的命令窗口 我在C:\ Program Files(x86)\ NodeJS下创建了一个目录 我通过cd C:\ Program Files(x86)\ NodeJS

导航命令窗口

我跑了命令     npm install connect

响应:     [编辑]&gt; npm -g install connect     错误的ERR! Windows_NT 6.1.7601     错误的ERR! argv&#34; [删除] \ node_modules \ NPM \ BIN \ NPM-cli.js&#34; &#34; -g&#34; &#34;安装&#34; &#34;连接&#34;     错误的ERR!节点v0.12.7     错误的ERR! npm v2.11.3     错误的ERR!代码ETIMEDOUT     错误的ERR!错误ETIMEDOUT     错误的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 install connect

同样的结果。日志文件包含:

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 npm@2.11.3
3 info using node@v0.12.7
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 connect@*
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。这个数百个要做的事情简单的文件就在我身边。

答案 7 :(得分:0)

我对express-http-proxy有同样的问题,解决方法是用npm安装软件包。起初我全局安装(-g),它的工作原理。在另一个安装我必须使用&#34; - save-dev&#34;。

$ 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
express-http-proxy@0.6.0 /opt/jet/nodejs/node-js/lib/node_modules/express-http-proxy
âââ type-is@1.6.6 (media-typer@0.3.0, mime-types@2.1.4)
âââ raw-body@1.3.4 (bytes@1.0.0, iconv-lite@0.4.8)

或者如果&#34; -g&#34;还不够

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

gulp作品

$ 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
...