internal/modules/cjs/loader.js:905 throw err; ^ Error: Cannot find module ‘cors’

When I run npm start

My result is ..

> nodefirebase_curd@1.0.0 start
> nodemon index.js

[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module 'cors'
Require stack:
- C:Userstong1Desktoptest-FirebasenodeFirebase_CURDindex.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (C:Userstong1Desktoptest-FirebasenodeFirebase_CURDindex.js:3:14)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\Users\tong1\Desktop\test-Firebase\nodeFirebase_CURD\index.js'
  ]
}

What wrong with this. I try to fix cors function but it doesn’t work.

'use strict';
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const config = require('./config');
const studentRoutes = require('./routes/student-routes');

const app = express()

app.use(express.json());
app.use(cors());
app.use(bodyParser.json());

app.use('./api', studentRoutes.routes);

app.listen(config.port, () => console.log('App is listening on url http://localhost:' + config.port));

This is my package.json.

{
  "name": "nodefirebase_curd",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "firebase": "^9.1.0",
    "firebase-admin": "^9.12.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.13"
  }
}

I’m struck in this problem with a long time. Can you help me, please.

Thank you so much.

Answer

you don’t have cors in dependencies

Install the dependency by npm i cors -P