8

I need your help on building rest API using express.. I have tried many versions of express. that also have CookieParser middleware but I'm just getting the above error...

  Error: Cannot find module 'cookie'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)`

I installed the cookie-parser globally and using Express version 4.... the code snippet is below.

var express = require('express'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
mongoose = require('mongoose'),
api = require('./routes/api.js') ,  
app = express();

app.use(session({ secret: 'angcms', saveUninitialized: true, resave: true })); app.use(cookieParser());

goldsoft25
  • 140
  • 1
  • 2
  • 10
  • Are you correctly require that module (require('cookie-parser'))? – Evgeny Samsonov Oct 15 '15 at 15:33
  • I thought require('cookie-parser') can also work... Well when I try to remove the cookie folder from the express node_modules and put in the global location , the problem disappears. – goldsoft25 Oct 19 '15 at 12:22

6 Answers6

9

I had the same situation. I solved by this way:

$ npm install

You must type this inside the Project folder. If i understood, "npm install" check the project dependencies registered on JSON files.

Mr. Junckes
  • 91
  • 1
  • 3
8

It sounds like npm has screwed up your module installation is all. Try reinstalling this module completely.

Uninstalling complete module

$ npm uninstall cookie-parser

after uninstalling the module, install it again using below command

$ npm install cookie-parser -g

Note: if you have not installed before then install first and the execute .

Umar Farooque Khan
  • 515
  • 1
  • 6
  • 17
4

You haven't install the typescript define of cookie-parser module, install @types/cookie-parser to solve it.

$ npm install @types/cookie-parser -D
syfless
  • 99
  • 4
2

To solve this problem, create another project and install cookie or open other project that have cookie installed and open node_module and copy the folder cookie and replace it.

This problem happens because you have npm and yarn installed if you open the node module folder, the cookie folder is empty because yarn or npm did not install the package

Pinaki Mukherjee
  • 1,616
  • 3
  • 20
  • 34
0

I had the same problem in Azure, no one of the solutions above worked. This one solved my problem:

npm i yarn -g
yarn add cookie-parser
SalmanShariati
  • 3,873
  • 4
  • 27
  • 46
0

I have had the issue and I am not sure what the reason was but what fixed it for me was to go in node_modules/cookie-parser and look at the package.json inside it. I then installed the dependancies listed there and it solved the problem.

(Those dependancies were cookie and cookie-signature)

cassepipe
  • 371
  • 6
  • 16