Running npm start on pm2 server returns Error: Cannot find module Cors
Running npm start on pm2 server returns Error: Cannot find module Cors
当我 运行 npm start
甚至 pm2 start npm --name "app name" -- start
命令时,我的应用程序 returns 出现这样的错误:
/home/cloud/sites/myapp$ npm start
> myapp@1.0.0 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! myapp@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myapp@1.0.0 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
这是我的 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"
}
}
还有我的目录树:
-myapp
-config
-node_modules
-routes
-utils
-validation
-package.json
-package-lock.json
-server.js
我试过删除 package-lock.json 和 node_modules,然后再次删除 运行 npm install
,甚至尝试 npm install cors
,它总是安装正常但是错误保持不变。
有什么想法吗?
替换它
const cors = require('Cors')
有了这个
const cors = require('cors')
当我 运行 npm start
甚至 pm2 start npm --name "app name" -- start
命令时,我的应用程序 returns 出现这样的错误:
/home/cloud/sites/myapp$ npm start
> myapp@1.0.0 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! myapp@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myapp@1.0.0 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
这是我的 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"
}
}
还有我的目录树:
-myapp
-config
-node_modules
-routes
-utils
-validation
-package.json
-package-lock.json
-server.js
我试过删除 package-lock.json 和 node_modules,然后再次删除 运行 npm install
,甚至尝试 npm install cors
,它总是安装正常但是错误保持不变。
有什么想法吗?
替换它
const cors = require('Cors')
有了这个
const cors = require('cors')