如何在 Heroku 安装后编译 Typescript?

How do I compile Typescript at Heroku postinstall?

我不想上传预编译的dist目录,而是想在服务器端编译src。

这是我在里面的脚本 package.json:

"scripts": {
    "test": "echo \"No test specified\" && exit 0",
    "start": "node dist/app.js",
    "postinstall": "tsc"
  }

依赖项如下:

"dependencies": {
    "@types/express": "^4.11.1",
    "@types/pg": "^7.4.4",
    "@types/socket.io": "^1.4.31",
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "pg": "^7.4.1",
    "socket.io": "^2.0.4",
    "tslint": "^5.9.1",
    "typescript": "^2.7.2"
  }

从'npm install will add the node_modules/.bin folder to the PATH environment variable during installation'开始,Heroku应该可以直接调用了

但这是我得到的错误:

Building dependencies
       Installing node modules (package.json + package-lock)

       > bilgi-yarismasi@1.0.0 postinstall /tmp/build_afa42c7943d4b71d2b48a016ae3b9e50
       > tsc

       sh: 1: tsc: not found
       npm ERR! file sh
       npm ERR! code ELIFECYCLE
       npm ERR! errno ENOENT
       npm ERR! syscall spawn
       npm ERR! bilgi-yarismasi@1.0.0 postinstall: `tsc`
       npm ERR! spawn ENOENT
       npm ERR!
       npm ERR! Failed at the bilgi-yarismasi@1.0.0 postinstall script.
       npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

       npm ERR! A complete log of this run can be found in:
       npm ERR!     /tmp/npmcache.LTxbD/_logs/2018-02-25T10_36_06_374Z-debug.log
-----> Build failed

您需要从 npm 脚本调用 tsc。否则 Heroku 会尝试找到一个名为 tsc 的全局依赖项。

package.json:

中创建一个新的 npm 脚本
"tsc": "tsc"

现在将 "postinstall": "tsc" 替换为:

"postinstall": "npm run tsc"

花了一些时间将我的简单打字稿 create-react-app 部署到 Heroku。这对我有用。

package.json - 根本不需要安装后

在命令行中为您的应用程序安装 buildpack 运行: 英雄联盟 buildpacks:add zidizei/typescript heroku buildpacks:add heroku/nodejs

您还可以搜索构建包 运行: heroku buildpacks:search 打字稿

我的服务器看起来像那样 (/root/server.js)

const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000

app.use(express.static('build'));
app.listen(PORT, () => console.log(`Listening on port ${PORT}`))

package.json

 "scripts": {
    "start": "node server.js",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

同样在推送到 heroku 之前,执行 运行 'npm run build'.

如果你要使用 webpack 开发服务器,我的解决方案将不起作用,它必须是自定义服务器,在我的例子中是 EXPRESS。

只需安装 typescript 作为依赖就可以了

Typescript 应作为开发依赖项安装

web: node server.js 在你的 procfile

确保将 npm build 添加为安装后脚本

告诉 npm 本地安装了 typescript 将解决未找到 tsc 的问题,因为 npm 正试图在 heroku 上全局找到它。

像这样。

"tsc": "./node_modules/typescript/bin/tsc",
"build": "tsc",
"postinstall": "npm run build",

我将 typescript 安装为 devDependency 在 Package.json:

"scripts": {
//other scripts
"build": "./node_modules/typescript/bin/tsc",
}