Build and Deply

Build The Project

When you are done developing the project, then you need to build your application.

Why we need to build our application?

We are using TypeScript at the development .. in the production we can not using TypeScript! so we need to build or compile our application to JavaScript files using TypeScript compiler or tsc .

To do that, run the following command inside your project's directory:

$ npm run build

when the build process done, you will see a new directory [dist] has been created inside your project's directory.

To start your production build locally, run the following:

npm start

If you are deploying your project with git like Heroku do, you don't need to build your app locally!! .. so you need to build the project after pushing it to the cloud [e.g. Heroku].

* Heroku automatically detects build script at package.json file and then runs it when pushing the project.

Deploy on Heroku

Deploying a Node.js application on Heroku is quite easy!

Let's deploy it :)

Heroku CLI

  1. Inside your over project directory, run the following command to login into Heroku CLI :

$ heroku login

2. Create Heroku app to push project's files on, by running the following command:

$ heroku create <app-name>

Configure MySQL Database for Node.js on Heroku app

3. Install Heroku's ClearDB add-on to your app with the following command:

ignite is the free plan of ClearDB (but requires Credit Card verification).

$ heroku addons:create cleardb:ignite

4. Get the URL of the database that we've just created :

$ heroku config | grep CLEARDB_DATABASE_URL

If you get the error on Window OS: 'grep' is not recognized as an internal or external command, operable program or batch file.

You can use the following command instead:

$ heroku config | findstr CLEARDB_DATABASE_URL

the URL will be similar to the following (but with your own credentials) :

mysql://<username>:<password>@<host>/<db-name>?reconnect=true

5. Update your project's database credentials in .env file with the credentials that we received so it should be like the following:

...

DB_HOST=<host>

DB_NAME=<db-name>

DB_USERNAME=<username>

DB_PASSWORD=<password>

and then save the file.

6. git add, commit and push your project's files to Heroku:

$ git add .
$ git commit -m "deployment"
$ git push heroku master

The push command will automatically install the dependencies and build your app as mentioned in the (Build The Project Section's hint) above.

7. Run project's migration for production with Heroku CLI:

$ heroku run npm run db:migrate:prod

8. Finally, open your Heroku app:

heroku open

this will open your application with your default browser.

That's it...

Now we have our project live with Heroku :)

Last updated