2013-03-12 45 views
11

Có vẻ như ứng dụng đơn giản này không thể tìm thấy mô-đun 'connect' sau khi tôi vừa cài đặt nó trong thư mục tệp.Lỗi: Không thể tìm thấy mô-đun 'kết nối'

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]6) 
└── [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) 

Trả lời

18

Express sử dụng nội bộ Connect là một trong những phụ thuộc node_modules của nó.

Nếu bạn muốn sử dụng Kết nối trong ứng dụng của mình, bạn cần thêm nó vào số package.json và chạy npm update.

Theo thiết kế, mỗi mô-đun nút có phụ thuộc nội bộ riêng của nó.

Nếu bạn muốn sử dụng bất kỳ ứng dụng nào, ứng dụng của bạn cũng sẽ cần cài đặt chúng.

Điều đó có nghĩa là ứng dụng của bạn có thể sử dụng phiên bản khác của mô-đun và mỗi mô-đun có thể sử dụng lại các phiên bản khác nhau mà không xung đột.

+0

Aha, tôi hiểu. Cảm ơn. – Architek1

+0

Nó hoạt động sau khi tôi thêm phụ thuộc explcit vào package.json và chạy bản cập nhật npm khi nó thực sự tải xuống gói kết nối tới cục bộ. Cảm ơn các mẹo – rahul

+0

Nếu bạn đang sử dụng Express, được xây dựng trên đầu trang của Kết nối, thì bạn nên sử dụng Express để khởi động máy chủ. Nếu bạn đang sử dụng Kết nối trực tiếp, bạn đang sử dụng Express để làm gì? Máy chủ thứ 2? –

1

vì bạn không cài đặt. bạn đã cài đặt express, không phải connect.

+0

Không phải mô-đun [email protected] được cài đặt với tốc độ cao như được liệt kê ở trên hay tôi có phải cài đặt riêng không? – Architek1

+2

có. nhưng bạn không thể 'require()' trừ khi bạn tự cài đặt nó một cách rõ ràng. đó là sự phụ thuộc của 'express', không phải của bạn. –

8

Kết nối là khung máy chủ HTTP có thể mở rộng Express sử dụng. Cụ thể Express sử dụng nó cung cấp hỗ trợ cho các phiên và xử lý cookie. Mã nguồn có sẵn trên github tại https://github.com/senchalabs/connect.

Nói chung khi một ứng dụng nút 'không thể tìm thấy', thứ đầu tiên cần thử là truy cập https://npmjs.org/package/npm-search và tìm kiếm những gì không thể tìm thấy. Sẽ rất khó để có được trong hệ thống sinh thái nút mà không cần sử dụng npm.

Trong trường hợp này, npmsearch sẽ tìm thấy kết nối và nếu bạn truy cập https://npmjs.org/package/connect, bạn sẽ tìm thấy hướng dẫn cài đặt cho mô-đun kết nối.

Trong trường hợp này:

npm install connect 

npm -g install connect 

cài đặt các mô-đun kết nối cho mỗi người dùng.

Bạn có thể cần phải root hoặc sử dụng sudo để thực hiện điều này trên hầu hết các bản phân phối Unix.

Hoặc bạn có thể thêm các phụ thuộc kết nối đến file package.json của dự án của bạn - tôi trông như thế này:

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

Tôi cũng đã phải sử dụng NPM để cài đặt các module đệm-crc32, phương pháp, debug, tươi , phạm vi phân tích cú pháp, cookie-chữ ký và cookie để có được một dự án làm việc thể hiện.

Chúc mừng nút hack :)

+0

cảm ơn đã làm việc cho tôi –

6

Các module kết nối hoặc không được cài đặt hoặc cài đặt một phần để cài đặt nó trước tiên và sau đó cố gắng để khởi động server nữa

npm install connect 
0

tôi đã cùng một vấn đề. Nút được cài đặt vào thư mục C:\Program Files (x86)\nodejs. Ban đầu tôi đã cố gắng để cài đặt và chạy các tập tin javascript từ thư mục này, mà gây ra nhiều vấn đề cho phép.

Nếu bạn đã đặt máy chủ trong thư mục đó, hãy thử di chuyển nó vào thư mục khác (có lẽ trong Documents), điều hướng trong dấu nhắc lệnh Nút và thử chạy lại.

Điều đó làm việc vượt trội hơn cho tôi.

0

(Win7) Tôi có cửa sổ Lệnh mở với tư cách quản trị viên Tôi đã tạo thư mục trong C: \ Program Files (x86) \ NodeJS Tôi điều hướng cửa sổ Lệnh qua cd C: \ Program Files (x86) \ NodeJS

tôi chạy lệnh NPM cài đặt kết nối

phản ứng: [redacted]> NPM -g cài đặt kết nối NPM ERR! Windows_NT 6.1.7601 npm ERR! argv "[redacted] \ node_modules \ npm \ bin \ npm-cli.js" "-g" "cài đặt" "kết nối" npm ERR! nút v0.12.7 npm ERR! npm v2.11.3 npm ERR! mã ETIMEDOUT npm ERR! errno ETIMEDOUT npm ERR! syscall kết nối

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 

Tôi chạy NPM cấu hình chỉnh sửa Tôi đã thay đổi dòng thuộc tính để loại bỏ các dấu chấm phẩy vào lúc bắt đầu của mỗi người dòng.

Một lần nữa, tôi chạy NPM -g cài đặt kết nối

kết quả tương tự. Tệp nhật ký có:

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 ] 

Tôi đoán tôi sẽ chỉ sử dụng IIS Express. Điều này hàng trăm tập tin để làm điều gì đó đơn giản là chỉ vượt ra ngoài tôi.

0

Tôi gặp vấn đề tương tự đối với express-http-proxy và giải pháp là cài đặt gói bằng npm. Lúc đầu, tôi cài đặt trên toàn cầu (-g) và nó hoạt động. Trong lần cài đặt khác, tôi phải sử dụng "--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) 

cài đặt gói

$ 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]) 

hoặc nếu "-g" là không đủ

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

ngụm làm việc

$ 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 
... 
Các vấn đề liên quan