簡體   English   中英

Mac 上的 Node.js Heroku 部署 - sh: 1: nodemon: not found / npm ERR! `nodemon fileName.js` / npm ERR! 在...啟動腳本失敗

[英]Node.js Heroku Deployment on Mac - sh: 1: nodemon: not found / npm ERR! `nodemon fileName.js` / npm ERR! Failed at the...start script

使用MacHeroku上部署Node.js

我的問題:

State changed from starting to crashed &&  
sh: 1: nodemon: not found &&  
Failed at...start script &&
status 1...code=H10

在使用React創建前端、后端服務器、 node.js / express.js和數據庫、 PostgreSQL ,我嘗試使用GitHeroku上部署我的服務器。 因為我已經有了Git ,所以我轉向了Heroku CLI

首先,從我服務器中的terminal ...

brew install heroku/brew/heroku
heroku create
git remote -v
git push heroku master

如果這不是您第一次使用Heroku ......

heroku git:remote -a theUrlYouWant
git push heroku master

...否則... Heroku動態地為您的應用分配一個端口,因此您無法將端口設置為固定數字。 Heroku 將端口添加到 env:

app.listen(process.env.PORT || 3000, () => {  
  console.log(`app is running on port ${process.env.PORT}`);
})  

...如果你添加了端口:

git add .
git commit -m "adding port"
git push heroku master

...最后,從我在服務器中的終端:

➜ folderName git:(master) heroku open  
➜ folderName git:(master) heroku logs --tail

2019-05-08T18:07:23.253827+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:07:25.323748+00:00 heroku[web.1]: State changed from starting to crashed  
2019-05-08T18:05:17.074233+00:00 app[web.1]: > nodemon fileName.js  
2019-05-08T18:05:17.074235+00:00 app[web.1]:   
2019-05-08T18:05:17.098124+00:00 app[web.1]: sh: 1: nodemon: not found  
2019-05-08T18:05:17.102512+00:00 app[web.1]: npm ERR! file sh  
2019-05-08T18:05:17.102801+00:00 app[web.1]: npm ERR! code ELIFECYCLE  
2019-05-08T18:05:17.103068+00:00 app[web.1]: npm ERR! errno ENOENT  
2019-05-08T18:05:17.103239+00:00 app[web.1]: npm ERR! syscall spawn    
2019-05-08T18:05:17.104259+00:00 app[web.1]: npm ERR! app@1.0.0 start: nodemon fileName.js  
2019-05-08T18:05:17.104361+00:00 app[web.1]: npm ERR! spawn ENOENT  
2019-05-08T18:05:17.104553+00:00 app[web.1]: npm ERR!  
2019-05-08T18:05:17.104692+00:00 app[web.1]: npm ERR! Failed at the app@1.0.0 start script.  
2019-05-08T18:05:17.104841+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 

[...]

2019-05-08T18:05:17.171915+00:00 heroku[web.1]: Process exited with status 1  
2019-05-08T18:05:37.338695+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=yourURL.herokuapp.com request_id=hidden fwd="ip" dyno= connect= service= status=503 bytes= protocol=https  

Heroku 默認在生產環境中運行,因此它不會安裝開發依賴項。

如果您不想將 nodemon 作為依賴項重新安裝,我認為您不應該這樣做,因為它的正確位置是在 devDependencies 中,而不是在依賴項中...

相反,您可以在package.json創建第二個 npm 腳本,通過僅在本地主機中運行nodemon來避免此錯誤:

"scripts": {
    "start": "node fileName.js",
    "start:dev": "nodemon fileName.js"
},  

當你想在本地運行項目時,只需在你的終端npm start:dev ,它就會用nodemon加載fileName.js

在 Heroku 中, npm start默認運行並從普通節點命令加載 fileName.js 並且您擺脫了該錯誤。

2019-05-08T18:13:40.319989+00:00 heroku[web.1]: State changed from crashed to starting  
2019-05-08T18:13:41.000000+00:00 app[api]: Build succeeded  
2019-05-08T18:13:42.658048+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:13:44.644005+00:00 app[web.1]: 
2019-05-08T18:13:44.644025+00:00 app[web.1]: > app@1.0.0 start /app  
2019-05-08T18:13:44.644027+00:00 app[web.1]: > node fileName.js  
2019-05-08T18:13:44.644028+00:00 app[web.1]:   
2019-05-08T18:13:45.158694+00:00 app[web.1]: app is running on port 33333  
2019-05-08T18:13:46.293205+00:00 heroku[web.1]: State changed from starting to up  
2019-05-08T18:13:47.788861+00:00 heroku[router]: at=info method=GET path="/" host=yourURL.herokuapp.com request_id=hidden fwd="ip" dyno=web.1 connect=0ms service=11ms status=200 bytes=245 protocol=https

我寫這篇文章是為了幫助你避免我花時間調試這個問題。

這對我有用:

在您的 Heroku 應用程序中,轉到Settings ,然后單擊Reveal Config Vars ,然后使用KEY NPM_CONFIG_PRODUCTIONValue false添加新記錄。

我將 nodemon 添加到 package.json 中的依賴項,現在它正在工作。

 "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "clarifai": "^2.9.1",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "knex": "^0.95.4",
    "pg": "^8.6.0", 
    "nodemon": "^2.0.7"
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM