[英]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 构建包,看起来它按以下顺序运行任务:
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.