cannot find module 'cookie' while using th

2019-08-08 00:28发布

问题:

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

回答1:

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 .



回答2:

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.



回答3:

To solve this probleme, create another projet 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