Cannot find module 'connect', when attempting to use require('connect').utils.parseCookie with nodejs
Asked Answered
C

2

5

I am relatively new to nodejs etc. Anyway I have a program that I am attempting to write that uses a session/authentication system based on this one here: http://iamtherockstar.com/blog/2012/02/14/nodejs-and-socketio-authentication-all-way-down/ Which is heavily based off this one i think (except updated for more recent versions) http://www.danielbaulig.de/socket-ioexpress/

(It utilises Node.js, Express.js, Redis & Sockets.io) Anyway, when I attempt to run my program I get this error:

Error: Cannot find module 'connect'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at Object.<anonymous> (/home/jez/webdir/app1/app.js:62:19)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)

The line of code this corresponds to is:

var parseCookie = require('connect').utils.parseCookie;

Now in both tutorials they use this line before calling on the method parseCookie(), however in neither of them do they talk about requireing the 'connect' module, only the express module which is built appon connect I believe. My program works perfectly with express, and will run fine before making the adjustments in the above tutorial.

I get the feeling it may be a recent update to node.js/express/socket.io however the tutorial was only written a few days ago?, Any help with this would be greatly appreciated!

Cartwell answered 27/2, 2012 at 10:50 Comment(0)
V
1

While it is possible to require the connect install that express has as a dependency, I recommend you also install connect in order to use the parseCookie utility. So just install connect in your local node_modules and you're good to go!

Vue answered 27/2, 2012 at 11:52 Comment(2)
I wondered if it was this simple, however I was very cautious about adding more dependencies as I already have about 5-6 and I have barely started my project. Thanks for the quick response, I will try this when I get home.Cartwell
While I understand your concern, connect is already a dependency of express, so it's not anything to worry about. I try to make sure I have the same version as express.Vue
F
7

Use cookie module

utils.parseCookies is no longer available. Instead you can use module cookie for this

var cookie = require('cookie');
var cookies = cookie.parse('foo=bar; cat=meow; dog=ruff');

https://github.com/shtylman/node-cookie

Footpace answered 2/8, 2012 at 11:36 Comment(0)
V
1

While it is possible to require the connect install that express has as a dependency, I recommend you also install connect in order to use the parseCookie utility. So just install connect in your local node_modules and you're good to go!

Vue answered 27/2, 2012 at 11:52 Comment(2)
I wondered if it was this simple, however I was very cautious about adding more dependencies as I already have about 5-6 and I have barely started my project. Thanks for the quick response, I will try this when I get home.Cartwell
While I understand your concern, connect is already a dependency of express, so it's not anything to worry about. I try to make sure I have the same version as express.Vue

© 2022 - 2024 — McMap. All rights reserved.