[英]Heroku node.js deployment
這是我第一次使用 heroku 部署 node.js 服務器,在本地 api 很有趣,但現在似乎 heroku 無法識別 nodemon (sh:1:論壇中的一個流行問題,但我沒有嘗試過),它似乎是論壇中的一個流行問題,但我沒有找到:評論,什么都沒有。 結果
這是我的 package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start":"nodemon server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"mongodb": "^4.8.0"
},
"engines":{
"node":"14.x"
}
}
這是 index.js:
import app from "./server.js"
import mongodb from "mongodb"
import dotenv from "dotenv"
import jobsDAO from "./dao/jobsDAO.js"
dotenv.config()
const MongoClient = mongodb.MongoClient
const port = process.env.PORT || 5000
MongoClient.connect(
process.env.JOBSFINDER_DB_URI,
{
wtimeoutMS: 2500,
}
)
.catch(err => {
console.error(err.stack)
process.exit(1)
})
.then(async client =>{
await jobsDAO.injectDB(client)
app.listen(port, () => {
console.log("listening on port",port)
})
})
這是 server.js:
import express from "express"
import cors from "cors"
import jobs from "./api/jobs.route.js"
const app = express()
app.use(cors())
app.use(express.json())
app.use("/api/v1/jobs", jobs)
app.use("*",(req,res)=>res.status(404).json({ error: "Not Found"}))
export default app
2022-08-24T20:58:55.624084+00:00 heroku[web.1]: Starting process with command `npm start`
2022-08-24T20:58:57.160097+00:00 app[web.1]:
2022-08-24T20:58:57.160109+00:00 app[web.1]: > backend@1.0.0 start /app
2022-08-24T20:58:57.160109+00:00 app[web.1]: > nodemon server
2022-08-24T20:58:57.160109+00:00 app[web.1]:
2022-08-24T20:58:57.170277+00:00 app[web.1]: sh: 1: nodemon: not found
2022-08-24T20:58:57.174407+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2022-08-24T20:58:57.174655+00:00 app[web.1]: npm ERR! syscall spawn
2022-08-24T20:58:57.174717+00:00 app[web.1]: npm ERR! file sh
2022-08-24T20:58:57.174792+00:00 app[web.1]: npm ERR! errno ENOENT
2022-08-24T20:58:57.177522+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `nodemon server`
2022-08-24T20:58:57.177629+00:00 app[web.1]: npm ERR! spawn ENOENT
2022-08-24T20:58:57.177682+00:00 app[web.1]: npm ERR!
2022-08-24T20:58:57.177719+00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2022-08-24T20:58:57.177754+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2022-08-24T20:58:57.182613+00:00 app[web.1]:
2022-08-24T20:58:57.182689+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2022-08-24T20:58:57.182729+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2022-08-24T20_58_57_178Z-debug.log
2022-08-24T20:58:57.299986+00:00 heroku[web.1]: Process exited with status 1
您的依賴項中缺少 nodemon。 運行npm install nodemon
將其添加到您的 package.json
您的依賴項中缺少 nodemon,需要使用npm install nodemon -D
添加(-D 是將其保存到開發依賴項)
Nodemon 通常用於未部署應用程序的開發環境。
看起來您是在 index.js 而不是 server.js 中啟動服務器
我建議以下
npm install nodemon -D
將您的腳本更改為:
"scripts": { "start":"node index.js", "start:dev": "nodemon index.js" "test": "echo \"Error: no test specified\" && exit 1" },
要在本地運行應用程序,您可以使用:
npm run start:dev
這將使用 nodemon 並在保存更改時刷新服務器。npm start
這將運行服務器(對生產有用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.