我开发了电影网站,并将其成功推送到Heroku使用node js和React完成,但未运行

问题描述

这是根package.json应用的react

    {
      "name": "movie-timeline","version": "0.1.0","private": true,"dependencies": {
        "@fortawesome/fontawesome-svg-core": "^1.2.30","@testing-library/jest-dom": "^4.2.4","@testing-library/react": "^9.3.2","@testing-library/user-event": "^7.1.2","axios": "^0.19.2","bootstrap": "^4.5.2","create-react-app": "^3.4.1","react": "^16.13.1","react-dom": "^16.13.1","react-icons": "^3.10.0","react-router-dom": "^5.2.0","react-scripts": "3.4.2","react-spring": "^8.0.27"
      },"scripts": {
        "dev": "set PORT=3006 && react-scripts start","start": "serve -s build","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject"
      },"proxy": "http://localhost:3006","eslintConfig": {
        "extends": "react-app"
      },"browserslist": {
        "production": [
          ...
        ],"development": [
          ...
        ]
      }
    }

添加set PORT=3006 && react-scripts start"脚本,以便可以在本地和Heroku上运行。

这是heroku记录的内容

$ heroku logs --tail

Warning: heroku update available from 7.38.2 to 7.42.6.
2020-08-12T22:32:51.450396+00:00 app[web.1]: npm ERR! errno ENOENT
2020-08-12T22:32:51.452680+00:00 app[web.1]: npm ERR! [email protected] start: `serve -s build`    
2020-08-12T22:32:51.452896+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-08-12T22:32:51.453058+00:00 app[web.1]: npm ERR!
2020-08-12T22:32:51.453205+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2020-08-12T22:32:51.453348+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-12T22:32:51.467442+00:00 app[web.1]: 
2020-08-12T22:32:51.467627+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-08-12T22:32:51.467740+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-08-12T22_32_51_460Z-debug.log
2020-08-12T22:32:51.556136+00:00 heroku[web.1]: Process exited with status 1
2020-08-12T22:32:51.706650+00:00 heroku[web.1]: State changed from starting to crashed   
2020-08-13T00:12:06.031766+00:00 heroku[web.1]: State changed from crashed to starting   
2020-08-13T00:12:29.908831+00:00 heroku[web.1]: Starting process with command `npm start`
2020-08-13T00:12:34.256591+00:00 app[web.1]: 
2020-08-13T00:12:34.256605+00:00 app[web.1]: > [email protected] start /app
2020-08-13T00:12:34.256606+00:00 app[web.1]: > serve -s build
2020-08-13T00:12:34.256606+00:00 app[web.1]: 
2020-08-13T00:12:34.267835+00:00 app[web.1]: sh: 1: serve: not found
2020-08-13T00:12:34.277638+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-08-13T00:12:34.277976+00:00 app[web.1]: npm ERR! syscall spawn
2020-08-13T00:12:34.278160+00:00 app[web.1]: npm ERR! file sh
2020-08-13T00:12:34.278407+00:00 app[web.1]: npm ERR! errno ENOENT
// ...

请问有人可以帮我吗?

解决方法

当您推送到Heroku时,它会在package.json应用中的react中运行此脚本:

{
  "scripts": {    
    "start": "serve -s build",// other
  },}

这已从heroku日志中得到确认...

// ...
2020-08-13T00:12:34.256605+00:00 app[web.1]: > [email protected] start /app
2020-08-13T00:12:34.256606+00:00 app[web.1]: > serve -s build
2020-08-13T00:12:34.256606+00:00 app[web.1]: 
2020-08-13T00:12:34.267835+00:00 app[web.1]: sh: 1: serve: not found
// ...

由于您在本地计算机上全局安装了serve,因此该项目得以运行。
因此,您需要在本地安装serveheroku才能在启动应用程序时运行。

$ npm install --save-dev serve

这样,heroku将能够运行您的"start"脚本。