<?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: Jotcomponents</title>
    <description>The latest articles on DEV Community by Jotcomponents (@jotcomponents).</description>
    <link>https://dev.to/jotcomponents</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%2F378871%2F07e8924c-f9dd-4948-a206-eaa80c471b4e.png</url>
      <title>DEV Community: Jotcomponents</title>
      <link>https://dev.to/jotcomponents</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jotcomponents"/>
    <language>en</language>
    <item>
      <title>How use .env files in Quasar</title>
      <dc:creator>Jotcomponents</dc:creator>
      <pubDate>Mon, 24 Aug 2020 13:03:55 +0000</pubDate>
      <link>https://dev.to/jotcomponents/how-use-env-files-in-quasar-1k55</link>
      <guid>https://dev.to/jotcomponents/how-use-env-files-in-quasar-1k55</guid>
      <description>&lt;p&gt;Many developers especially beginers have problems with proper use of .env file during Quasar application development and build-up.&lt;/p&gt;

&lt;p&gt;Quasar recent documentation (for ver.1.13) mentions three methods for using enviroment variables in the application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CLI inline variables e.g.&lt;br&gt;
&lt;code&gt;QENV=development quasar dev&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@quasar/dotenv extension&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@quasar/quasar-app-extension-dotenv"&gt;https://www.npmjs.com/package/@quasar/quasar-app-extension-dotenv&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@quasar/qenv extension&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@quasar/quasar-app-extension-qenv"&gt;https://www.npmjs.com/package/@quasar/quasar-app-extension-qenv&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unfortunatelly despite descriptions in official documentation&lt;br&gt;
&lt;a href="https://quasar.dev/quasar-cli/handling-process-env"&gt;https://quasar.dev/quasar-cli/handling-process-env&lt;/a&gt;&lt;br&gt;
&lt;a href="https://quasar.dev/quasar-cli/quasar-conf-js#Example-setting-env-for-dev%2Fbuild"&gt;https://quasar.dev/quasar-cli/quasar-conf-js#Example-setting-env-for-dev%2Fbuild&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;developers are confused in many cases when they try to use .env file due to missing important information in Quasar ducumentation. In this article I want to clarify how proper to use the enviromental variables.&lt;/p&gt;
&lt;h2&gt;
  
  
  Two basic use cases
&lt;/h2&gt;

&lt;p&gt;Because of different scope of operation we can split the described methods in two groups:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;applicable in quasar.conf.js (main configuration file):

&lt;ul&gt;
&lt;li&gt;CLI inline variables&lt;/li&gt;
&lt;li&gt;@quasar/dotenv extension&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;applicable ONLY in the application (boot files, components):

&lt;ul&gt;
&lt;li&gt;@quasar/qenv extension&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most important conclusion here is&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You cannot use @quasar/qenv extension for settings in quasar.conf.js&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use @quasar/dotenv extension
&lt;/h2&gt;

&lt;p&gt;Very good description is in article&lt;br&gt;
&lt;a href="https://medium.com/carbono/using-env-file-in-quasar-apps-72b56909302f"&gt;https://medium.com/carbono/using-env-file-in-quasar-apps-72b56909302f&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first install Quasar App Extension dotenv:&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/@quasar/quasar-app-extension-dotenv"&gt;https://www.npmjs.com/package/@quasar/quasar-app-extension-dotenv&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;quasar ext add @quasar/dotenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then create new js file envparser.js in /src/config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const DotEnv = require('dotenv')
const parsedEnv = DotEnv.config().parsed
module.exports = function () {
  // Let's stringify our variables
  for (key in parsedEnv) {
    if (typeof parsedEnv[key] === 'string') {
      parsedEnv[key] = JSON.stringify(parsedEnv[key])
    }
  }
  return parsedEnv
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;in quasar.conf.js then include on the most top of the file following code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const envparser = require('./src/config/envparser.js')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and assign this function to (new) env variable to the build attribute in quasar.conf.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build: {
  env: envparser(),
  vueRouterMode: 'history',
...}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a file &lt;strong&gt;.env&lt;/strong&gt; in the top (root) project directory. It can contain any number of custom env variables e.g.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JOT_PUBLIC_PATH=http://dev.com/flexbox/dist/spa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then you use the variable in quasar.config.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build: {
     env: envparser(),
     vueRouterMode: 'history',
     publicPath: ctx.prod ? process.env.JOT_PUBLIC_PATH : '',
...}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In object process.env are available all server/computer environment variables and all custom environment variables from .env file.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use @quasar/qenv extension
&lt;/h2&gt;

&lt;p&gt;Install Quasar extension&lt;/p&gt;

&lt;p&gt;&lt;code&gt;quasar ext add @quasar/qenv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;File .quasar.env.json is created during installation with demo template&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "development": {
    "ENV_TYPE": "Running Development",
    "ENV_DEV": "Development"
  },
  "production": {
    "ENV_TYPE": "Running Production",
    "ENV_PROD": "Production"
  },
  "test": {
    "ENV_TYPE": "Running Test",
    "ENV_Test": "Test"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Variables from .quasar.env.json are available only after successfull application build (i.e. after processing quasar.config.js). These custom variables together with some other built-in variables are available in the application boot files, application and components files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In object process.env are available only variables from .quasar.env.json and Quasar built-in environment variables.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using above defined template the process.env object contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
      "ENV_TYPE": "Running Development",
      "ENV_DEV": "Development",
      "NODE_ENV": "development",
      "CLIENT": true,
      "SERVER": false,
      "DEV": true,
      "PROD": false,
      "MODE": "spa",
      "VUE_ROUTER_MODE": "hash",
      "VUE_ROUTER_BASE": "",
      "APP_URL": "http://localhost:8080"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After build processing the server/computer environment variables are NOT AVAILABLE in new created process.env object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example use in boot file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default async (app /* { app, router, Vue ... } */) =&amp;gt; {
 console.log('show router mode:', process.env.VUE_ROUTER_MODE);
 // something to do
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example use in *.vue file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
  console.log('inside', process.env.VUE_ROUTER_MODE);
  export default {
    name: 'App',
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>quasar</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
