The environment variable process.env.variable works perfectly fine during local development, however, it seems to malfunction once the application is deployed to Heroku within

Currently, I am utilizing nuxt.js and attempting to display images dynamically. To achieve this, I have implemented the BASE_URL variable within the .env file, allowing me to access image files based on the set BASE_URL in my local environment.

.env file

BASE_URL = https://pctool.herokuapp.com
DB_HOST = dummy_host_name
DB_USER = dummy_user_name
DB_NAME = dummy_database_name
PASSWORD = dummy_password

in image.vue file

template

<img class="pic-0" :src="makeImagePath(product.image[0])"/>

script

methods: {
    makeImagePath (img_name) {
      return process.env.BASE_URL + "/product/" + img_name;
    }
}

The output in the local environment is https://i.stack.imgur.com/tcPOu.png

However, upon deploying the code to Heroku, it seems that the environmental variable is not functioning correctly.

heroku env

Heroku environment variables

The deployed output is:

https://i.stack.imgur.com/OGWtr.png

If I hardcode the URL as shown below, it works flawlessly:

script

methods: {
    makeImagePath (img_name) {
      return "https://pctool.herokuapp.com/product/" + img_name;
    }
}

Nevertheless, by hardcoding the URL, it becomes static instead of dynamic. This means I would need to manually modify the URL from local to production each time I implement this concept within my project. Hence, I am seeking a way to maintain dynamic URLs to prevent any conflicts during deployment.

Answer №1

It is generally recommended not to include your .env file in version control. Instead, you can configure environment variables in your Heroku app settings.

https://i.stack.imgur.com/GnNSJ.png

You can set these variables in the environment:

BASE_URL = https://example.herokuapp.com
DB_HOST = example_host_name
DB_USER = example_user_name
DB_NAME = example_database_name
PASSWORD = example_password

Answer №2

After some investigation, I finally cracked the code. To access .env variables on the client side, you must first define them in the Heroku Config Vars settings. But don't forget to also include this snippet in your nuxt.config.js file, as shown below:

publicRuntimeConfig: {
    apiUrl: process.env.API_URL,
    apiKey: process.env.API_KEY
}

Once this is set up, you can easily retrieve these variables like so: this.$config.apiUrl

Just a friendly reminder, refrain from exposing any sensitive data such as API keys through this method, since client-side scripts are visible to the public eye. Stay safe!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Connecting socket.io to a Vue.js single page application using Javascript

I am new to Vue.js, Socket.io, and Node.js. Recently, I developed a single-page application that allows communication between a Node.js server running on a Raspberry Pi and a Siemens S7-1500 PLC connected via Ethernet. The Raspberry Pi acts as a WiFi acces ...

When running the command "babel server --out-dir build", Babel does not compile .graphql files located within the build directory

Introduction: Our application utilizes Heroku as the server platform. After pushing code, it triggers npm start according to the package.json file. My current nodeJS version on my laptop is v8.16.2, with npm at 6.4.1. In the package.json file: "engines ...

Are there any guidelines available for me to follow when launching a site built on node.js?

As I wrap up the development of my first website using node.js, I am wondering if there is a checklist to ensure all necessary tasks are completed before launching it. During development, when unexpected values are encountered in my database queries (using ...

Utilizing Vue.js to fetch information from a post API in Node.js

I'm currently working on developing a post API in Node.js, with Vue.js as my frontend framework and using vue-resource to interact with APIs. Here is the submit function I have implemented on the frontend: validateBeforeSubmit() { var self = this ...

How to Implement Dropdowns with a vue.js Router-Link

I have set up a vue.js router and I am currently using the provided structure to insert links from an array. These links are displayed horizontally on the page. However, I am interested in changing these simple links into dropdown menus instead. Is it po ...

error encountered: script not found during starting the application, leading to application crash when attempting to deploy on Heroku

I am a beginner in the world of coding, but I am eager to learn so that I can create a chatbot for my Facebook page. Below are the scripts I have inside each .js file and the errors I encountered. After running 'heroku open', the web displays an ...

Heroku Deploy with NestJS - Troubleshooting H10 Error

After deploying my NestJS Application to Heroku, I encountered the dreaded H10 Error. Here's the log: 2020-05-07T13:12:51.967622+00:00 heroku[web.1]: State changed from starting to crashed 2020-05-07T13:15:14.556288+00:00 heroku[router]: at=error c ...

Error: Heroku Unable to Locate Module (Model File)

I encountered a model import error in my Node.js application while deploying to Heroku. 2016-02-10T20:40:04.712617+00:00 app[web.1]: > node ./bin/www 2016-02-10T20:40:04.712615+00:00 app[web.1]: > <a href="/cdn-cgi/l/email-protection" class="__cf ...

Initializing start scripts in the package.json file

When launching my react app locally, I need to execute three commands: cd react-web npm run postinstall export REACT_APP_CUSTOMER_ENVIRONMENT=xxx npm start After running these commands, the application server starts on localhost:3000. For the start script ...

Is it possible for me to hand over control to a child process and retrieve the result in Node.js?

Recently, I encountered an issue where multiple simultaneous GET requests to my Node.js server caused it to become overwhelmed and unresponsive, leading to timeouts for clients (503, service unavailable). Upon thorough performance analysis, I discovered t ...

Creating automatic page additions using Node.js on Heroku?

Can you assist me with a challenge I'm facing? Currently, I am using node.js and heroku to deploy an application and each time I add a new post, I have to manually update my web.js file. This process was manageable when I had fewer pages, but now it&a ...

Encountered a SyntaxError while deploying Nuxt.js SSR on Passenger: The import statement cannot be used outside a module

I am currently in the process of deploying my Nuxt app on a hosting service that uses Passenger to run Node.js applications. After building the app with the command ">npm run build" and deploying the content from the .nuxt folder onto the server, specif ...

Running two instances of the Express server within a single application

Currently, I am running two separate express servers within a single Node.JS application. The purpose is to have 2 distinct sockets for running 2 different services simultaneously. However, there is a limitation with Google Cloud and Heroku as they do no ...

Unable to execute requirejs on heroku platform

Looking for some advice on using nodejs + requirejs on heroku. The project structure is quite common: \ |-\app |-\build |-\config |-\node-modules |-\public | |-server.js |-etc... After running "gulp build", the build folder ...

Troubleshooting steps for resolving a node.js error during execution

I recently delved into server side programming with node.js, but I'm encountering some issues when trying to execute it. My server is set up at 127.0.0.1:80, however, I keep running into errors. Console: Server running at http://127.0.0.1:80/ node:ev ...

I'm currently attempting to deploy an application to Heroku using Git Bash, but I am encountering difficulty in accessing the app on the Heroku platform

Having trouble deploying an app from git-bash to heroku, as I am unable to open the app on heroku. Every time I try to run the npm install command, I encounter an error. npm install Below is the error message that I receive: npm WARN EBADENGINE Unsupporte ...

What is the best method for storing a third-party image in cache?

Running my website, I aim to achieve top-notch performance scores using LightHouse. I have successfully cached all the images I created (Cache-Control: public, max-age=31536000). Unfortunately, third-party website images are not cached. How can I cache t ...

Exploring the possibilities of updating the version dynamically in package.json file

Upon receiving version 1.0.1 (server_version) from the server I make sure process.env.version matches server_version If they are the same, I update process.env.version to server_version. But unfortunately, this process cannot be done from the client side. ...

Error occurs when using Express.js in combination with linting

https://www.youtube.com/watch?v=Fa4cRMaTDUI I am currently following a tutorial and attempting to replicate everything the author is doing. At 19:00 into the video, he sets up a project using vue.js and express.js. He begins by creating a folder named &apo ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...