node.js - 如何将我的 Typescript Node.js 应用程序部署到 Heroku?

标签 node.js typescript heroku

在本地测试时,我之前运行过:

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

然后我想我的 Procfile 应该是这样的:

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

但它说找不到模块“typescript”,即使它在 package.json 中。我在一些地方读到 ts-node 不是部署到 Heroku 的方式,所以我不确定该怎么做。

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

web: ./node_modules/.bin/tsc --module commonjs --allowJs --outDir build/ --sourceMap --target es6 index.ts && node build/index.js

这成功了,但是当实际运行它时,我正在使用的一堆库得到“找不到模块'...'”。

最佳答案

或者,您可以将 TypeScript 编译为安装后 Hook ,并将 node build/index.js 作为唯一的 Procfile 命令运行:

您的 package.json 应该包含在 npm install 之后和 Node 进程启动之前执行的安装后提示:

"scripts": {
  "start": "node build/index.js",
  "build": "tsc",
  "postinstall": "npm run build"
}

然后您可以按原样保留您的 Procfile:

web: npm start

Heroku 记录了这种“基于部署构建”的方法 here .

关于node.js - 如何将我的 Typescript Node.js 应用程序部署到 Heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43317980/

相关文章:

javascript - 访问本地主机时index.js无法获取/:5000

angular - 打印 Angular 8 中的键值

Angular 7 : Inject and use Services globally for all Components

heroku - 从 '@babel/plugin-proposal-decorators' 找不到模块 '/app'

python - 如何在不使用 Djstatic 和 Whitenoise 的情况下添加静态文件?

node.js - 如何提高redis sub的性能?

node.js - 如何终止node.js上的子进程?

javascript - Axios POST 到服务器返回空字符串

node.js - 如何正确构造和使用套接字缓冲区?

node.js - Node.JS 的 Heroku 错误代码 H12 问题,后端使用 MongoDB atlas