我试图用一些getter和setter来部署nodejs应用程序,但无法部署。我选择了heroku上的nodejs环境,并提供了enviriment变量但它显示出与express相关的错误。
Jul 24 10:18:04 PM node:internal/modules/cjs/loader:949
Jul 24 10:18:04 PM throw err;
Jul 24 10:18:04 PM ^
Jul 24 10:18:04 PM
Jul 24 10:18:04 PM Error: Cannot find module 'express'
Jul 24 10:18:04 PM Require stack:
- /opt/render/project/src/index.js
Jul 24 10:18:04 PM at Module._resolveFilename (node:internal/modules/cjs/loader:946:15)
Jul 24 10:18:04 PM at Module._load (node:internal/modules/cjs/loader:787:27)
Jul 24 10:18:04 PM at Module.require (node:internal/modules/cjs/loader:1012:19)
Jul 24 10:18:04 PM at require (node:internal/modules/cjs/helpers:102:18)
Jul 24 10:18:04 PM at Object.<anonymous> (/opt/render/project/src/index.js:1:17)
Jul 24 10:18:04 PM at Module._compile (node:internal/modules/cjs/loader:1112:14)
Jul 24 10:18:04 PM at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
Jul 24 10:18:04 PM at Module.load (node:internal/modules/cjs/loader:988:32)
Jul 24 10:18:04 PM at Module._load (node:internal/modules/cjs/loader:834:12)
Jul 24 10:18:04 PM at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
Jul 24 10:18:04 PM code: 'MODULE_NOT_FOUND',
Jul 24 10:18:04 PM requireStack: [ '/opt/render/project/src/index.js' ]
Jul 24 10:18:04 PM }
Jul 24 10:18:04 PM
Jul 24 10:18:04 PM Node.js v18.5.0
Package.json
{
"name": "linktree_back",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node index.js --ext js",
"dev": "nodemon index.js --exec 'npm start -s"
},
"dependencies": {
"@sendgrid/mail": "^7.7.0",
"axios": "^0.27.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-validator": "^6.14.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.3.5",
"mongoose-unique-validator": "^3.1.0",
"node": "^18.5.0"
},
"keywords": [
"example",
"heroku"
],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.19"
}
}
我在index.js中导入这样的expressconst express = require("express");
也在服务器中
mongoose
.connect(process.env.MONGO_URL)
.then(() => {
app.listen(process.env.PORT || 5000);
})
.catch((err) => {
console.log(err);
});
尝试运行npm install,这将确保package.json中指定的所有包都已安装。然后将您的应用程序部署到Heroku。或者,如果你知道你只缺少express,只需通过运行自行安装express
npm install express
您的应用程序正在查找快递模块,但找不到它,因为它不存在。