如何将Typescript Node.js应用程序部署到Heroku? [英] How do I deploy my Typescript Node.js app to Heroku?

查看:104
本文介绍了如何将Typescript Node.js应用程序部署到Heroku?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本地测试时,我以前正在运行:

When testing locally I was previously running:

"build-live": "nodemon --exec ./node_modules/.bin/ts-node -r dotenv/config -- ./index.ts"

然后我发现我的Procfile应该是这样的:

I then figured my Procfile should be something like:

web: ./node_modules/.bin/ts-node -- ./index.ts

但是它说找不到模块'typescript',即使它在package.json中也是如此.我在几个地方读到,ts-node并不是部署到Heroku的方法,所以我不确定该怎么做.

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.

更新:我想我应该编译它,所以我尝试了:

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

这成功了,但是当实际运行它时,我正在使用的一堆lib却显示找不到模块'...'".

This succeeds, however when actually running it, a bunch of the libs I'm using get "Cannot find module '...'".

推荐答案

您给Heroku提供的命令是通过编译index.ts和依赖项的起始节点来启动Web进程" index.js.根据计时的方式,在节点启动时index.js可能存在或不存在.

The command you've given Heroku is to launch the web "process" by compiling index.ts and dependencies and starting node at index.js. Depending on how things are timed, index.js might or might not exist at the time node starts.

您需要在启动应用程序之前就已经编译好源代码.例如,网络应该为web: node 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.

每个构建过程都不同,因此您需要为自己的设置弄清楚.但是,假设您有一个经典的设置,即在其中按下git,然后Heroku拾取该更改并使用新的插件更新应用程序.您可以只在本地编译内容,并在存储库中包含index.js和任何其他生成的输出,以便在Slug中可以使用它供Heroku使用.

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.

更好的方法是使用与Heroku集成的构建服务器.在此处进行构建后,对其进行配置以将构建结果发送到Heroku. Travis 具有这样的简单设置.这样,您就无需在存储库中包含构建输出,这被认为是一种反模式.

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. Travis has a straighforward setup like this. This way you don't need to include build outputs in your repository, which is considered an anti-pattern.

在边节点上,尝试使用tsconfig.json保持tsc配置.这样一来,您就不必在整个地方编写这么长的命令行.

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.

这篇关于如何将Typescript Node.js应用程序部署到Heroku?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆