如何将我的 Typescript Node.js 应用程序部署到 Heroku?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43317980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I deploy my Typescript Node.js app to Heroku?
提问by atkayla
When testing locally I was previously running:
在本地测试时,我以前运行过:
"build-live": "nodemon --exec ./node_modules/.bin/ts-node -r dotenv/config -- ./index.ts"
I then figured my Procfile should be something like:
然后我想我的 Procfile 应该是这样的:
web: ./node_modules/.bin/ts-node -- ./index.ts
But it says module 'typescript' not found, even when it is in package.json
. I read in a few places that ts-node
is not the way to go to deploy to Heroku, so I am not sure what to do.
但它说找不到模块“打字稿”,即使它在package.json
. 我在几个地方读到ts-node
不是部署到 Heroku 的方式,所以我不知道该怎么做。
UPDATE: I think I am supposed to compile it, so I tried:
更新:我想我应该编译它,所以我试过:
web: ./node_modules/.bin/tsc --module commonjs --allowJs --outDir build/ --sourceMap --target es6 index.ts && node build/index.js
This succeeds, however when actually running it, a bunch of the libs I'm using get "Cannot find module '...'".
这成功了,但是在实际运行它时,我正在使用的一堆库得到“找不到模块'...'”。
采纳答案by Horia Coman
The command you've given Heroku is to launch the web "process" by compiling index.ts and dependencies andstarting node at index.js. Depending on how things are timed, index.js might or might not exist at the time node starts.
您给 Heroku 的命令是通过编译 index.ts 和依赖项并在 index.js 处启动节点来启动网络“进程” 。根据时间的不同,index.js 在节点启动时可能存在也可能不存在。
You need to already have your sources compiled by the time you want to start your app. For example, web should just be web: node index.js
or similar.
在启动应用程序时,您需要已经编译好源代码。例如, web 应该只是web: node index.js
或相似。
Each build process is different, so you need to figure that out for your own setup. But, suppose you have a classical setup where you push to git and then Heroku picks up that change and updates the app with the new slug. You could just compile things locally and include index.js and any other build output in the repository, for it to be available in the slug for Heroku to use.
每个构建过程都不同,因此您需要为自己的设置弄清楚这一点。但是,假设您有一个经典的设置,您推送到 git,然后 Heroku 获取更改并使用新的 slug 更新应用程序。您可以只在本地编译内容并将 index.js 和任何其他构建输出包含在存储库中,以便在 Heroku 使用的 slug 中提供它。
A better approach is to use a build server which has an integration with Heroku. After you do the build there, configure it to send the build results to Heroku. Travishas a straighforward setup like this. This way you don't need to include build outputs in your repository, which is considered an anti-pattern.
更好的方法是使用与 Heroku 集成的构建服务器。在那里完成构建后,将其配置为将构建结果发送到 Heroku。Travis有这样一个简单的设置。这样你就不需要在你的存储库中包含构建输出,这被认为是一种反模式。
On a sidenode, try using a tsconfig.json
to keep the tsc configuration. It will save you from having to write such long command lines all over the place.
在侧节点上,尝试使用 atsconfig.json
来保留 tsc 配置。它将使您不必到处编写如此长的命令行。
回答by Fabian
Alternatively you can have the TypeScript compile as a postinstall hook and run node build/index.js
as the only Procfile command:
或者,您可以将 TypeScript 编译为安装后挂钩并node build/index.js
作为唯一的 Procfile 命令运行:
Your package.json
should contain a postinstall hint that gets executed after npm install
and before the node process launches:
您package.json
应该包含npm install
在节点进程启动之后和之前执行的安装后提示:
"scripts": {
"start": "node build/index.js",
"build": "tsc",
"postinstall": "npm run build"
}
You can then leave your Procfile as is:
然后,您可以按原样保留 Procfile:
web: npm start
This 'build on deploy' approach is documented by Heroku here.
Heroku在这里记录了这种“基于部署构建”的方法。
回答by Vukasin Sevo
Fabian said that we could do something like:
Fabian 说我们可以这样做:
"scripts": {
"start": "node build/index.js",
"build": "tsc",
"postinstall": "npm run build"
}
As of me writing this, I tested this and can state: postinstall
is not required since build script is ran by Heroku. If you want to do it without build script, then you can use heroku-postbuild which will run after dependencies are installed there you run tsc
to compile.
在我写这篇文章的时候,我测试了这个并且可以声明:postinstall
不需要,因为构建脚本是由 Heroku 运行的。如果您想在没有构建脚本的情况下执行此操作,则可以使用 heroku-postbuild,它将在安装依赖项后运行tsc
以进行编译。