Why cors module can not be found?

Viewed 29

I'm trying to enable cors for my next.js API.

Based on docs and the example I installed cors:

npm i cors

And I can verify that it's in my package.json:

"cors": "^2.8.5",

And it's in my node_modules directory.

However, when I use import Cors from 'cors' in my api code, I get this error:

Module not found: Can't resolve 'cors'

Why do I get this error while the package is installed? I also tried const cors = require('cors') and got the same error.

1 Answers

First of all you can try delete node modules and package-lock.json:

rm -rf node_modules
rm -f package-lock.json
npm cache clean --force

npm install

After this be sure that cors is in your dependencies on package.json:

"dependencies": {
    "cors": "version",
  },

if not you can try to manually add the line with desired version and run npm install

If still not working you can try:

npm install cors --save

in your main project directory

Related