3
votes

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());

5
Are you correctly require that module (require('cookie-parser'))?Evgeny Samsonov
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

5 Answers

4
votes

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 .

4
votes

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.

1
votes

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

$ npm install @types/cookie-parser -D
1
votes

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

0
votes

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