{"id":1927,"date":"2021-07-25T02:51:50","date_gmt":"2021-07-25T02:51:50","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1927"},"modified":"2022-09-09T14:01:05","modified_gmt":"2022-09-09T14:01:05","slug":"deploy-node-js-app-on-heroku","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/javascript\/deploy-node-js-app-on-heroku\/","title":{"rendered":"Deploy Node.js app on heroku"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/09\/mongodb-heroku-atlas-3.jpg\"><img loading=\"lazy\" width=\"1748\" height=\"995\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2022\/09\/mongodb-heroku-atlas-3.jpg\" alt=\"\" class=\"wp-image-2426\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Signup\/Signin on heorku<\/p>\n\n\n\n<p><a href=\"https:\/\/herokuapp.com\/\">https:\/\/herokuapp.com\/<\/a><\/p>\n\n\n\n<p>Login from terminal (Make sure you have installed heroku cli &#8211; <a href=\"https:\/\/devcenter.heroku.com\/articles\/heroku-cli#download-and-install\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/devcenter.heroku.com\/articles\/heroku-cli#download-and-install<\/a>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku login<\/code><\/pre>\n\n\n\n<p>Create app.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const express = require(\"express\");\nconst app = express();\nconst port = 8080;\n\napp.get(\"\/\", (req, res) =&gt; {\n    res.send(\"Hello Heroku\");\n})\n\napp.listen(<strong>process.env.PORT<\/strong> || port, () =&gt; {\n\tconsole.log(\"listening 8080...\");\n});<\/code><\/pre>\n\n\n\n<p><strong>process.env.PORT<\/strong> this will be provided by heroku server<\/p>\n\n\n\n<p>Test locally by running following command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node app.js\nOR\nnodemon app.js<\/code><\/pre>\n\n\n\n<p>If you get any error e.g. module not found you can install those module using npm<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install &lt;module_name&gt;<\/code><\/pre>\n\n\n\n<p>To find installed module version<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm view &lt;module_name&gt; version\ne.g.\nnpm view express version<\/code><\/pre>\n\n\n\n<p>Create package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n\t\"scripts\" : {\n\t\t\"start\" : \"node app.js\"\n\t},\n\t\"dependencies\": {\n\t\t\"express\": \"4.17.1\",\n\t\t\"mongoose\": \"5.13.3\",\n\t\t\"cors\": \"2.8.5\"\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Run following command from terminal<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#onetime\ngit init<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#onetime\nheroku create &lt;yournewappname&gt;<\/code><\/pre>\n\n\n\n<p>Run git commands<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add .\ngit commit -m 'msg'\n\n#to verify origin\ngit config -l\n\n#if you are not able to see url and fetch then run git remote add origin \n#remote.heroku.url=https:\/\/git.heroku.com\/project.git\n#remote.heroku.fetch=+refs\/heads\/*:refs\/remotes\/heroku\/*\n#git remote add origin heroku_git_url\n#git push origin master\n\ngit push heroku master<\/code><\/pre>\n\n\n\n<p>Once app is deployed it will show you an url which you can access publicly from internet.<\/p>\n\n\n\n<p>To see error logs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku logs --tail<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Signup\/Signin on heorku https:\/\/herokuapp.com\/ Login from terminal (Make sure you have installed heroku cli &#8211; https:\/\/devcenter.heroku.com\/articles\/heroku-cli#download-and-install) Create app.js process.env.PORT this will be provided by heroku server Test locally by running following command If you get any error e.g. module not found you can install those module using npm To find installed module version Create package.json [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1927"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1927"}],"version-history":[{"count":17,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1927\/revisions"}],"predecessor-version":[{"id":2427,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1927\/revisions\/2427"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}