在 heroku 上部署 typeorm/typescript 个应用

Deploying typeorm/typescript app on heroku

我在 heroku 上部署 typeorm 应用程序时遇到问题。这是此应用程序的完全简约版本: https://github.com/wiktorkujawa/typeorm-heroku

所以我做了什么:

  1. 开始项目 typeorm init --name typeorm-heroku --database postgres

服务器(typeorm-postrges 文件夹)

  1. 在服务器文件夹(cd typeorm-postgres)中键入: npx tsconfig.json(选择例如节点) 创建了 yarn.lock 文件并更新了它 ...

已安装服务器依赖项。

index.tsx中的简单连接 添加了 Procfile 文件(什么都没改变)

客户

  1. 创建了 React 项目并为它们安装了依赖项: npx create-react-app web --typescript 纱加... 2.Create src 文件夹内的 graphql 文件夹并初始化 graphql 代码生成器 npx graphql-codegen 初始化

    你的模式在哪里?:http://localhost:4000/graphql 你的操作和片段在哪里?: src/graphql/*.graphql 在哪里写输出:(让它默认) ...(默认) package.json 中的哪个脚本应该 运行 codegen?一代

在codegen.yml中添加:

config:
       hooksImportFrom: '@apollo/react-hooks'
       withHooks: true
       withHOC: false
       withComponent: false

并安装了 codegen 的依赖项: 纱 3.生成模式: 纱线代 4. 在 index.tsx 中从 https://www.apollographql.com/docs/react/migrating/boost-migration/#advanced-migration

复制代码
  1. 阅读简单列表查询以检查查询是否有效,但它仅在开发模式下有效。 在 heroku 中,我得到了很大的 heroku 页面:

Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command heroku logs --tail

这是在终端中输入 heroku logs --tail 后的错误:

2020-06-28T18:40:57.079023+00:00 app[web.1]: > typeorm-heroku@0.0.1 start /app
2020-06-28T18:40:57.079024+00:00 app[web.1]: > ts-node src/index.ts
2020-06-28T18:40:57.079024+00:00 app[web.1]:
2020-06-28T18:40:57.085699+00:00 app[web.1]: sh: 1: ts-node: not found
2020-06-28T18:40:57.095880+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-28T18:40:57.096505+00:00 app[web.1]: npm ERR! syscall spawn
2020-06-28T18:40:57.097800+00:00 app[web.1]: npm ERR! file sh
2020-06-28T18:40:57.098220+00:00 app[web.1]: npm ERR! errno ENOENT
2020-06-28T18:40:57.099843+00:00 app[web.1]: npm ERR! typeorm-heroku@0.0.1 start: `ts-node src/index.ts`
2020-06-28T18:40:57.100130+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-06-28T18:40:57.100384+00:00 app[web.1]: npm ERR!
2020-06-28T18:40:57.100546+00:00 app[web.1]: npm ERR! Failed at the typeorm-heroku@0.0.1 start script.
2020-06-28T18:40:57.100713+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-06-28T18:40:57.119552+00:00 app[web.1]:
2020-06-28T18:40:57.120040+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-06-28T18:40:57.120170+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-06-28T18_40_57_105Z-debug.log
2020-06-28T18:40:57.195532+00:00 heroku[web.1]: Process exited with status 1
2020-06-28T18:40:57.257902+00:00 heroku[web.1]: State changed from starting to crashed
2020-06-28T18:41:03.656226+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=typeorm-heroku.herokuapp.com request_id=4e810375-5365-47c5-bccd-a6b28a893359 fwd="178.235.225.93" dyno= connect= service= status=503 bytes= protocol=https
2020-06-28T18:41:04.010781+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=typeorm-heroku.herokuapp.com request_id=1a30a549-55a7-49af-a6b5-f199ee78fda0 fwd="178.235.225.93" dyno= connect= service= status=503 bytes= protocol=https

嗯,我认为问题出在package.json,但我不知道如何解决。

你的节点包版本是什么,你可以通过输入 npm -v 来检查它,如果它是新的,比如 v12.xx 尝试降级到 v10.16 并在 package.json

删除node_modules文件夹和运行npm install重新安装所有项目依赖

解决方案是在依赖项(而不是 devDependencies)中安装 ts-node。

使用docker将后端服务器部署到heroku。并在 Vercel 上部署前端。这就是我通常部署 typescript 应用程序的方式。您可以按照 https://github.com/benawad/lireddit/blob/master/server/Dockerfile

中给出的设置 docker 服务器