Home > Mobile >  Deploying TypeScript Express Server to Heroku
Deploying TypeScript Express Server to Heroku

Time:12-08

In attempting to deploy an Express TypeScript server to Heroku, I am encountering an error. I ran heroku logs --tail and this is the output:

2021-12-02T21:09:20.152292 00:00 app[web.1]: > backend@1.0.0 start /app
2021-12-02T21:09:20.152292 00:00 app[web.1]: > ts-node src/index.ts
2021-12-02T21:09:20.152292 00:00 app[web.1]: 
2021-12-02T21:09:20.164793 00:00 app[web.1]: sh: 1: ts-node: not found
2021-12-02T21:09:20.176470 00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-12-02T21:09:20.176764 00:00 app[web.1]: npm ERR! syscall spawn
2021-12-02T21:09:20.176897 00:00 app[web.1]: npm ERR! file sh
2021-12-02T21:09:20.177050 00:00 app[web.1]: npm ERR! errno ENOENT
2021-12-02T21:09:20.184008 00:00 app[web.1]: npm ERR! backend@1.0.0 start: `ts-node src/index.ts`
2021-12-02T21:09:20.184048 00:00 app[web.1]: npm ERR! spawn ENOENT
2021-12-02T21:09:20.184122 00:00 app[web.1]: npm ERR!
2021-12-02T21:09:20.184186 00:00 app[web.1]: npm ERR! Failed at the backend@1.0.0 start script.
2021-12-02T21:09:20.184240 00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-12-02T21:09:20.189489 00:00 app[web.1]: 
2021-12-02T21:09:20.192700 00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-12-02T21:09:20.192779 00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-12-02T21_09_20_184Z-debug.log
2021-12-02T21:09:20.372397 00:00 heroku[web.1]: Process exited with status 1
2021-12-02T21:09:20.745674 00:00 heroku[web.1]: State changed from starting to crashed

From my own research, it was suggested I put in a buildpack for TypeScript, the one that comes up on heroku buildpacks:search is zidizei/typescript. I had installed that without success.

Further research suggested heroku node/js buildpack. I ran heroku buildpacks:set heroku/nodejs and tried that without success. So I switch back to zidizei, everything compiles and builds, except for the server start.

I'm confused as to what the root of the problem is, and I think I may have gotten turned around.

Additional steps I've taken:

// package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node src/index.ts",
    "dev": "ts-node-dev src/index.ts",
    "build": "tsc",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
    "db:generate-migration": "yarn typeorm -- migrate:generate --config ormconfig.ts --connection --name",
    "db:run-migration": "yarn typeorm -- migration:run"
// Procfile

web: npm start

Alternatively, are there any strong heroku contenders for hosting an express server and a Postgres DB?

Adding: I noticed that when I entered into the environment using heroku run bash --app <my_app_name> that the dist folder is being built. Is the solution to point my start script to something like node dist/src/index.js?

CodePudding user response:

Okay, so for anyone else that is reading this.

My solution was:

In package.json all I had to do was change my start script from

"start": "ts-node src/index.ts",

to

"start": "node dist/src/index.js",

Seems that at some point I got turned around in my documentation and was looking every which way except the obvious one.

  • Related