How do I compile Typescript at Heroku postinstall?

无人久伴 提交于 2020-12-29 09:34:03

问题


Instead of uploading the precompiled dist directory, I want to compile src at server side instead.

Here are my scripts inside package.json:

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

Here are the dependencies:

"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"
  }

Since 'npm install will add the node_modules/.bin folder to the PATH environment variable during installation', Heroku should be able to call it directly.

But here is the error I get:

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

回答1:


You need to call tsc from an npm script. Otherwise Heroku tries to find a global dependency named tsc.

Create a new npm script in your package.json:

"tsc": "tsc"

now replace "postinstall": "tsc" with:

"postinstall": "npm run tsc"



回答2:


Spent a while to deploy my simple typescript create-react-app to Heroku. Here what worked for me.

package.json - does not need postinstall at all

In the command line install buildpack for your application run: heroku buildpacks:add zidizei/typescript heroku buildpacks:add heroku/nodejs

You can also search for buildpacks run: heroku buildpacks:search typescript

My server looks like that (/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"
  }

Also before pushing to heroku, do run 'npm run build'.

My solution wont work if you gonna use webpack dev server, it must be custom server, in my case it was EXPRESS.



来源:https://stackoverflow.com/questions/48972663/how-do-i-compile-typescript-at-heroku-postinstall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!