简体   繁体   English

部署到 heroku 时,在 typescript 中找不到 tsc 表达和反应应用程序

[英]tsc not found in typescript express and react app when deploying to heroku

I am working on an express app(in typescript) and a react app bootstrapped with create-react-app(in js).我正在开发一个快速应用程序(在打字稿中)和一个用 create-react-app(在 js 中)引导的反应应用程序。 The directory structure is here .目录结构在这里 The server directory contains code for server which is inside server/src and is written in typescript(output into server/dist on building). server 目录包含server/src内的 server 代码,并用 typescript 编写(在构建时输出到server/dist )。 Similarly the code for react is inside /src and on building generates the /build directory.同样,react 的代码在/src中,并且在构建时会生成/build目录。

The problem is when deploying to heroku, the command tsc is not working and thus build fails The stack trace is -问题是部署到 heroku 时,命令 tsc 不起作用,因此构建失败堆栈跟踪是 -


2020-11-29T10:11:12.328622+00:00 app[web.1]: > cd server && yarn start
2020-11-29T10:11:12.781818+00:00 app[web.1]: yarn run v1.22.10
2020-11-29T10:11:12.832721+00:00 app[web.1]: $ tsc & node .
2020-11-29T10:11:12.850144+00:00 app[web.1]: /bin/sh: 1: tsc: not found
2020-11-29T10:11:12.905249+00:00 app[web.1]: internal/modules/cjs/loader.js:303
2020-11-29T10:11:12.905252+00:00 app[web.1]: throw err;
2020-11-29T10:11:12.905253+00:00 app[web.1]: ^
2020-11-29T10:11:12.905253+00:00 app[web.1]: 

How should i include it in the package.json(s) so that this command is recognised in the heroku deployment environment.我应该如何将它包含在 package.json(s) 中,以便在 heroku 部署环境中识别该命令。

Package.json scripts for React app is React 应用程序的 Package.json 脚本是

"scripts": {
    "startClient": "react-scripts start",
    "buildClient": "react-scripts build",
    "build": "react-scripts build && (cd server && yarn install)",
    "start":"cd server && yarn start",
    "devServer":"cd server && yarn dev",
    "tsc": "tsc",  // 1)
    "postinstall": "yarn tsc" // 2)
  },

1 & 2 were added following the suggestion in How do I compile Typescript at Heroku postinstall? 1 & 2 是按照如何在 Heroku 安装后编译 Typescript 中的建议添加的? . .

The package.json script for server app is服务器应用程序的 package.json 脚本是

"scripts": {
    "build":"tsc",
    "start":"tsc & node .",   // this is the command which fails
    "dev":"tsc -w & nodemon ."
  },

i know i have to configure that postinstall so that tsc is recognised but i am not able to understand in which of the package.json's scripts i should add it to?我知道我必须配置该 postinstall 以便识别tsc ,但我无法理解我应该将它添加到 package.json 的哪个脚本中? Would appreciate if you could clarify on which one to add it to and why is that so that tsc isn't recognised.如果您能澄清将其添加到哪个以及为什么会导致tsc无法识别,将不胜感激。 Alternatively, can i directly use npx tsc or is that not recommended?或者,我可以直接使用npx tsc还是不推荐?

I ran into a similar issue and am not certain its the same as what you encountered but posting anyways in case its helpful to someone else.我遇到了类似的问题,不确定它是否与您遇到的相同,但无论如何都会发布,以防它对其他人有帮助。

I'm using the heroku/nodejs build pack and it looks like it runs tasks in the following order:我正在使用 heroku/nodejs 构建包,看起来它按以下顺序运行任务:

  • Creating runtime environment创建运行时环境
  • ... ...
  • Installing dependencies安装依赖项
  • Build建造
  • Pruning devDependencies修剪 devDependencies
  • ... ...

After the build completes successfully, the container is started with the command npm 'start' which in your case calls the tsc command which no longer exists if you installed it as a dev dependency (since it was pruned).构建成功完成后,使用命令 npm 'start' 启动容器,在您的情况下,该命令调用 tsc 命令,如果您将其安装为开发依赖项,则该命令不再存在(因为它已被修剪)。

My solution was to move tsc out of the start step and leave it as node.我的解决方案是将tsc移出启动步骤并将其保留为node.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Heroku上托管带有打字稿和React应用的Express时的问题 - Issues when Hosting express with typescript and react app on Heroku 将 TypeScript Express 服务器部署到 Heroku - Deploying TypeScript Express Server to Heroku “您需要启用 JavaScript 才能运行此应用程序。” 将 React/Express 应用程序部署到 heroku 时出错 - "You need to enable JavaScript to run this app." error when deploying React/Express app to heroku webpack:未找到 - 将我的应用程序部署到 heroku 时 - webpack: not found -When deploying my app to heroku 将 Angular 应用程序部署到 Heroku 时出现“未找到”错误 - 'Not Found' error when deploying Angular app to Heroku 将 Web 应用程序部署到 Heroku 时找不到 Express 模块 - Express Module not found upon deploying web app to Heroku Heroku - 部署 React/Express 应用程序并在请求 chunk.js 文件时在控制台中获取“无法加载资源” - Heroku - Deploying React/Express app and getting 'Failed to load resource' in the console when requesting the chunk.js files Heroku在部署react(Node)应用程序时出错 - Error in Heroku when deploying react (Node) app 将 React + Express 应用程序部署到 Heroku 导致“渲染未返回任何内容” - Deploying React + Express app to Heroku resulting in "Nothing was returned from render" 将 React Express 和 Node 应用程序部署到 Heroku 代理错误 - Deploying React Express and Node app to Heroku Proxy Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM