<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kristof Balinth</title>
    <description>The latest articles on DEV Community by Kristof Balinth (@kristof92).</description>
    <link>https://dev.to/kristof92</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F628815%2F8d51eb75-3c45-4a29-b951-c3a30ab524d1.jpeg</url>
      <title>DEV Community: Kristof Balinth</title>
      <link>https://dev.to/kristof92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kristof92"/>
    <language>en</language>
    <item>
      <title>Easy Deployment to Heroku - step by step (Node &amp; Vue app)</title>
      <dc:creator>Kristof Balinth</dc:creator>
      <pubDate>Mon, 12 Jul 2021 11:55:06 +0000</pubDate>
      <link>https://dev.to/kristof92/how-to-deploy-a-mevn-app-to-heroku-step-by-step-571g</link>
      <guid>https://dev.to/kristof92/how-to-deploy-a-mevn-app-to-heroku-step-by-step-571g</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclaimer: this post is just a mere reflection of my personal experience while building and deploying a web-application using the MEVN (MongoDB, Express.js, Vue.js, Node.js) stack&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article is not a "best practice" or anything like this- this is just one of the possibly many ways to achieve the same goal- a working, deployed webapp&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I am really curious about your thoughts- please leave a comment below after reading it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Okay, so...&lt;/p&gt;

&lt;p&gt;Initially I created a project with two completely separate applications in it- one Vue and one Node app. My project structure looked like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2sdcuapkuxb6mcpxxoze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2sdcuapkuxb6mcpxxoze.png" alt="Initial project structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both apps were running on different ports and I just proxied http calls from client to server via vue.config.js.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6u2qz7u5j8kv9hsb23s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6u2qz7u5j8kv9hsb23s.png" alt="devserver-proxy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was working well in the development phase however I had difficulties when I tried to make a production build.&lt;/p&gt;

&lt;p&gt;To cut a long story short- &lt;strong&gt;Heroku does not like a project structure like this&lt;/strong&gt;. Heroku needs to have a package.json file in the root, so I had to restructure my app like this (I placed my Vue app inside the Node app):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhwst79h3xicap5ueqp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhwst79h3xicap5ueqp3.png" alt="new project structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly, I created the dist folder of client, by running the &lt;code&gt;npm run build&lt;/code&gt; command (creates the production build of your Vue.js app) and then copied over the content of dist dir. to a freshly created dir. at server-side: build directory.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; you don't have to do this, you can just generate the dist folder basically anywhere you just have to be able to point at it in app.js of Node app.&lt;br&gt;
I did it this way for better code-readibility&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After that, I modified my app.js of Node app with a single line (serving up Vue app as static content) at line 11:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fianlufktl9fuj36gaut5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fianlufktl9fuj36gaut5.png" alt="server/app.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point it is really easy to check if Heroku will accept your creation or not.&lt;br&gt;
Modify your "start" script by allowing it to utilise your environment variables listed in a .env file in your project like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fpxh953fa5to03efbj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3fpxh953fa5to03efbj8.png" alt="modified start script"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now running &lt;code&gt;npm run start&lt;/code&gt; in root should start your Node app which serves up your Vue.js app meaning that by visiting &lt;a href="http://localhost/NODE_PORT" rel="noopener noreferrer"&gt;http://localhost/NODE_PORT&lt;/a&gt; domain, you should be able interact with your application.&lt;/p&gt;

&lt;p&gt;After the successful test, &lt;strong&gt;don't forget to reformat your "start" script&lt;/strong&gt; (Heroku will try to run that) like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjkh7y0v4fla87470qfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjkh7y0v4fla87470qfm.png" alt="start script the right way"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next step is to check if you have any dependency which is saved as a dev dependency but should be stored as regular dependency.&lt;/p&gt;

&lt;p&gt;If it's all good and everything hums together, don't forget to push your code to GitHub and you are basically ready for Heroku deployment.&lt;/p&gt;

&lt;p&gt;Install heroku &lt;em&gt;(&lt;a href="https://devcenter.heroku.com/articles/heroku-cli" rel="noopener noreferrer"&gt;https://devcenter.heroku.com/articles/heroku-cli&lt;/a&gt;)&lt;/em&gt;, log in and issue the &lt;code&gt;heroku create PROJECTNAME&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Now you should &lt;strong&gt;add your environment variables&lt;/strong&gt; with the &lt;code&gt;heroku config:set key=value&lt;/code&gt; command.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt;: On UNIX/LINUX environment you might have to add the value in single quotes if it has any single characters&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget, &lt;strong&gt;you don't have to add PORT&lt;/strong&gt; as an env. var., Heroku will take care of that.&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;heroku config&lt;/code&gt; command you can double-check if you have all the env. variables needed.&lt;/p&gt;

&lt;p&gt;When it's all good, issue order 66...ehmm I mean command &lt;code&gt;git push heroku main&lt;/code&gt; and it's all done.&lt;/p&gt;

&lt;p&gt;If your app crashes and won't start, issue the &lt;code&gt;heroku logs --tail&lt;/code&gt; command- &lt;strong&gt;heroku logs are super-useful&lt;/strong&gt;, I was able to successfully troubleshoot when I had to based on the logs.&lt;/p&gt;

&lt;p&gt;Thats it- let me know if I missed out something important.&lt;/p&gt;

&lt;p&gt;Happy deploy! ✨&lt;/p&gt;

</description>
      <category>vue</category>
      <category>node</category>
      <category>heroku</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
