Search code examples
node.jsexpressnpmserverpm2

Running npm start on pm2 server returns Error: Cannot find module Cors


When I run npm start or even pm2 start npm --name "app name" -- start command, my app returns an error like this:

/home/cloud/sites/myapp$ npm start 

> [email protected] start /home/cloud/sites/myapp
> node server.js

internal/modules/cjs/loader.js:818
  throw err;
  ^

Error: Cannot find module 'Cors'
Require stack:
- /home/cloud/sites/myapp/server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/cloud/sites/myapp/server.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/cloud/sites/myapp/server.js' ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-05-12T07_31_36_119Z-debug.log

This is my package.json:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "* TEST\r * Login validation",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "crypto-random-string": "^3.3.1",
    "express": "^4.17.1",
    "is-empty": "^1.2.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.11.17",
    "socket.io": "^4.0.1",
    "validator": "^13.5.2"
  }
}

And my directory tree:

-myapp
 -config
 -node_modules
 -routes
 -utils
 -validation
 -package.json
 -package-lock.json
 -server.js

I've tried deleting package-lock.json and node_modules and then run npm install again, even tried npm install cors, it always installs fine but the error stays the same. Any ideas?


Solution

  • Replace it

    const cors = require('Cors')
    

    With this

    const cors = require('cors')