How do I compile Typescript at Heroku postinstall?
Asked Answered
T

8

19

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)

       > [email protected] 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! [email protected] postinstall: `tsc`
       npm ERR! spawn ENOENT
       npm ERR!
       npm ERR! Failed at the [email protected] 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
Tart answered 25/2, 2018 at 10:58 Comment(1)
You can also check my answerSpindell
C
28

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"
Carliecarlile answered 25/2, 2018 at 11:19 Comment(4)
Exactly! Thanks for saving my day, uhm week.Ghats
it's not working on bitbucket pipeline, how can i solve?Morville
Probably it is missing TSC. Dev dependency only?N
@MarekUrbanowicz but dev dependency should still work, the doc says dev dependences are kept for the duration of install and build steps, or I am missing something?Lorianne
A
10

Typescript should be installed as a devdependency

have web: node server.js in your procfile

make sure to add npm build as a postinstall script

Telling npm that typescript is installed locally will fix tsc not found issue, since npm is trying to find it globally on heroku.

like this.

"tsc": "./node_modules/typescript/bin/tsc",
"build": "tsc",
"postinstall": "npm run build",
Acevedo answered 13/5, 2021 at 10:30 Comment(1)
This should be marked as the correct answer. It is definitely better than the current marked correct answer.Odin
B
3

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.

Burgoo answered 12/9, 2020 at 3:6 Comment(1)
The whole purpose we use post-install is to not to run the build script everytime we push the repo.Ghats
S
1

just install typescript as dependency it will work

Synergist answered 26/3, 2021 at 6:58 Comment(1)
Typescript should be added as a dev dependency. You only need to tell npm that typescript is installed locally by adding this to your script "tsc": "./node_modules/typescript/bin/tsc",Acevedo
N
0

For me in package.json

"scripts": {
    "tsc": "./node_modules/typescript/bin/tsc",
    "postinstall": "npm run tsc"
  },

Works for me.

Najera answered 27/7, 2022 at 16:55 Comment(0)
H
0

There are 2 solutions that I found viable for this problem:

  1. Move typescript from devDependencies to dependencies object in your package.json
  2. Install a Heroku buildpack on your app which can run the ts command. You can search Heroku buildpacks on elements.heroku.com. The procedure to add is very simple.
Heilner answered 25/1, 2023 at 21:22 Comment(0)
H
0

I tried all the answers here too to fix the sh: 1: tsc: not found error, but the only thing I could get to work was to move typescript from dev dependency to dependency.

Haggerty answered 13/3 at 20:47 Comment(1)
You just need to not rely on tsc at runtime. As long as you build your application at build time and try to run plain JavaScript at runtime this won't be a problem.Joeljoela
P
-1

I installed typescript as devDependency and in Package.json:

"scripts": {
//other scripts
"build": "./node_modules/typescript/bin/tsc",
}
Poetize answered 24/4, 2022 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.