0

I want to deploy a Node.js server made with TypeScript on Heroku. But this does not work. I have read many tutorials and other questions on Stack Overflow, but none of them work. Below I post my code for tsconfig.json and package.json.

package.json


  "main": "build/index.js",
  "scripts": {
    "start": "node build/index.js",
    "dev": "nodemon server.js",
    "build-ts": "tsc",
    "start:dev": "nodemon --config \"./nodemon.json\"",
    "test": "echo \"Error: no test specified\" && exit 1",
    "heroku-postbuild": "npm run build-ts"
  },
  "keywords": [],
  "author": "Gabriel Meyer",
  "license": "MIT",
  "devDependencies": {
    "@types/cors": "^2.8.6",
    "@types/express": "^4.17.3",
    "@types/node": "^12.12.31",
    "nodemon": "^2.0.2",
    "prettier": "^2.0.2",
    "ts-node": "^8.8.1",
    "tslib": "^1.11.1",
    "tslint": "^6.1.0",
    "typescript": "^3.8.3"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "uuid": "^7.0.2",
    "websocket": "^1.0.31"
  }

tsconfig.json


{
  "compilerOptions": {
    "module": "commonjs",
    "strict": true,
    "baseUrl": ".",
    "rootDir": "src",
    "outDir": "build",
    "sourceMap": true,
    "removeComments": true,
    "experimentalDecorators": true,
    "target": "ES6",
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "strictPropertyInitialization": false,
    "types": ["node"],
    "typeRoots": ["node_modules/@types"]
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

index.ts


import express from 'express';
const app = express();
const port: string | number = process.env.PORT || 5000;
app.use('*', (req, res) => {
  res.send('<h1>Welcome to your server!</h1>');
});

// create a server object:
app.listen(port, () => console.log(`hosting @${port}`));
1
  • Do you have the logs from heroku? If you have the cli installed, you can use the command heroku logs --tail to get the logs. Commented Apr 18, 2020 at 11:14

1 Answer 1

2

Now I have found the answer:

My mistake was, that I have put all Typescript-related dependencies into the devDependencies. Heroku needs typescript, ts-node and tslib as a regular dependency, to transpile .ts-files to .js-files. See below:

package.json

"dependencies": {
    "ts-node": "^8.8.1",
    "tslib": "^1.11.1",
    "typescript": "^3.8.3",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "uuid": "^7.0.2",
    "websocket": "^1.0.31"
} 
Sign up to request clarification or add additional context in comments.

1 Comment

This wasn't a mistake. The better approach is to introduce a few extra steps into your heroku build process, like so: 1) install ALL packages (including devdeps) 2) tsc 3) npm prune --production (removes dev dependencies). There are heroku buildbacks which do this, or depending on your project, you can do this yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.