在本地测试时,我之前运行过:
"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/