LoginSignup
4

More than 5 years have passed since last update.

Angular CLI で作成したアプリをHerokuへデプロイする方法

Posted at

事前準備

事前準備は以下を参考に作成する
Herokuにアカウント登録してアプリを作成する

ng new で作成したPJにすること

express をインストール

cd your-app-name
npm install express --save

pacage.jsonの記述を変更する

  • scripts内のstartの内容をnode server.jsに変更
  • scripts内にpostinstallを追加
  "scripts": {
    "start": "node server.js",
    "postinstall": "ng build --aot --target=production"
  },
  • devDependenciesに記述されているangular系とtypescriptをdependenciesへ移動する

  • enginesを追加する

  "engines": {
    "node": "6.10.3",
    "npm": "3.10.10"
  }

参考のpackage.json

{
  "name": "your-app-name",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot --target=production"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "1.2.7",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/animations": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "core-js": "^2.4.1",
    "express": "^4.15.3",
    "ng2-bootstrap": "^1.6.3",
    "rxjs": "^5.4.1",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4"
  },
  "engines": {
    "node": "6.10.3",
    "npm": "3.10.10"
  }
}

server.jsを作成する

  • your-app-name 直下に作成する
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist'));
app.listen(process.env.PORT || 8080);
const path = require('path');
app.get('/*', function(req, res){
  res.sendFile(path.join(__dirname + '/dist/index.html'));
});
console.log('Server listening on port 8080');

Heroku へデプロイ

heroku login
# gitが初めての場合、git initする

heroku git:clone -a [heroku-app-name]
cd your-app-name
git add .
git commit -am "make it better"
git push heroku master

参考コード GitHub

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4