How do I compile Typescript at Heroku postinstall?

前端 未结 2 1420
故里飘歌
故里飘歌 2021-02-19 19:20

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

Here are my scripts inside package.json:

\"scripts\":          


        
相关标签:
2条回答
  • 2021-02-19 19:36

    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"
    
    0 讨论(0)
  • 2021-02-19 19:40

    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.

    0 讨论(0)
提交回复
热议问题