Having trouble retrieving process variable in Vue3JS Vite project

In my Vue3 application, developed with Vite, I am integrating with a Solidity smart contract on Ropsten using web3js. Additionally, I am utilizing web3.storage to store images on IPFS, with the API key stored in a `.env` file at the project root:

VUE_APP_API_TOKEN=VALUE
VITE_API_TOKEN=VALUE

The issue arises as web3.storage requires the API token to be accessed through `process.env`, but the global `process` variable is not defined within my application. This results in an error: `Uncaught ReferenceError: process is not defined`.

My assumption is that this problem stems from using Vite instead of pure Vue3. I attempted to export `process.env` in the `vite.config.ts` file with the following code, however, it did not resolve the issue:

export default ({ mode }) => {
   process.env = { ...process.env, ...loadEnv(mode, process.cwd(), '') };

   console.log(process.env.VITE_API_TOKEN)         //Works fine: VALUE is logged
   console.log(process.env.VUE_APP_API_TOKEN)      //Works fine: VALUE is logged

   return defineConfig({
       plugins: [vue()]
   });
}

How can I access the `process` variable from my Vue files to retrieve the environment variable values and enable web3.storage functionality?

Answer №1

.env

VITE_WEB3_STORAGE_TOKEN="your_token"

SomeComponent.vue: (or any other file within your application, really):

console.log(import.meta.env.VITE_WEB3_STORAGE_TOKEN) // "your_token"

Please Note: according to the Vite documentation, only variables that begin with VITE_ will be accessible to your Vite-processed code:

To avoid unintentionally exposing environment variables to the client-side, only variables prefixed with VITE_ are made available to your Vite-compiled code.

Answer №2

When executing the build script in a CI environment, it is essential to ensure that you establish or populate the necessary .env file prior to initiating the build process.

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

When receiveMessage is triggered, the FCM push notification fails to refresh the view

After following this helpful tutorial on Push Notifications with Angular 6 + Firebase Cloud Messaging, everything is working perfectly. I am able to receive notifications when using another browser. To ensure that my notification list and the length of no ...

Is it possible to prevent users from clicking on a jQuery UI Datepicker?

Is it possible to disable the functionality of a jQuery UI Datepicker so that clicking on it does nothing but display the date? I attempted: $("#calendar").datepicker({ disabled: true, altField: '#note_date', maxDate: 0 }); Unfortu ...

Angular 1: Handling Multiple Conditions and Exclusions Based on Other Conditions

I have currently added an additional span to accommodate one condition. <div ng-repeat="(key, resultLinks) in resultGroup.resultLinks"> <div ng-if="key < 4 || viewMore" ng-repeat="(subKey, linksWrap) in resultLinks.linksWrap"> & ...

Node.js is essential when using Angular for implementing ui-select components

I'm currently delving into learning AngularJS. I've successfully created a basic web application using AngularJS, with Java EE powering the backend (server side). This app is being hosted on Tomcat. The advantages of AngularJS over JQuery are bec ...

I need to figure out how to incorporate a specific feature into my personal list by leveraging Javascript

Looking at the list below, it's a simple one that allows users to add and remove items while also changing their order using buttons. However, I want to enhance this feature by removing the "up" button for the first item and the "down" button for the ...

Ways to identify browser version in Angular 4 to discourage IE usage

Is there a method in Angular 4 (TypeScript) for detecting the browser type? I am currently working with Angular 4 and would like to explore options for identifying the browser type when my application is loaded. I specifically want to prevent my applicati ...

What steps should I follow to enable webview autocomplete for a login form on Ionic's Capacitor in iOS?

I am trying to figure out how to activate the autocomplete feature for a login form in Capacitor when using Ionic React. The issue arises when bundling the web app in Capacitor, as the autocomplete functionality seems to disappear. Although it works on Saf ...

Having trouble transitioning from Curl to Axios in Node.js? Encountering issues when trying to make a curl request using the provided certificates, resulting in ECONNRESET or

I've been struggling to make a successful curl request in Axios (Node.js) without any luck. A typical working curl request I use (can't provide exact details as the API is private) is like this: curl --cacert server.pem --cert client.pem:123pas ...

Choose a collection of elements and encase them within a <div> tag

I am currently working on creating a greasemonkey script for a webpage that has a rather challenging structure. My goal is to show and hide different entries, but the content is formatted like this: <a name="first"/> <h3>First</h3> Some ...

What is the best approach for resolving this asynchronous task sequencing issue in JavaScript?

Below is a code snippet where tasks are defined as an object and the function definition should ensure the expected output is met. Let tasks = { ‘a’: { job: function(finish){ setTimeout(() => { ...

The useEffect function seems to be malfunctioning within the functional component, resulting in the display of empty data

There seems to be an issue with UseEffect not retrieving data from the API. When I attempt to log it to the console, it shows null........................................ import React, { useEffect, useState } from "react"; import axios from &qu ...

Exploring nested JSON data in Vue.js

In my attempt to access nested JSON within an array using Vue for a simple search functionality, I encountered a problem. Each school is encapsulated in a "hit" array, causing the system to perceive only one result of "hit" instead of returning data for ea ...

Looking for a way to incorporate a dropdown menu on my webpage for inputting database information seamlessly through

I am having trouble identifying the mistake in my code. I want a dropdown in my form to trigger an onSelect event that calls an AJAX script, which then queries the database and populates the company address information on the form. Currently, the dropdown ...

Executing JavaScript code on ASP.NET page load

Inside my HTML code, there is a radio box styled using ASP.NET RadioButtonList with specific attributes. The second list item is set to be selected by default, however, the problem arises when the page loads as the function dis() is not being called. I wan ...

The problem arises when trying to use the Jquery click event on buttons that have been generated

I've encountered an issue where I can create components dynamically, but the click event doesn't seem to work on buttons that are dynamically generated. Check out the code snippet below: HTML file: <div class="merge_sections">&l ...

What is the best way to display the weather information for specific coordinates in ReactJS?

I've been working on a code that retrieves farm details based on longitude and latitude. My goal now is to fetch the weather information for that specific farm using openweathermap However, each time I attempt to do so, an error message {cod: '4 ...

Merge together JQuery variables

I want to assign a unique number to each JavaScript variable and jQuery element in my code. Take a look at the snippet below: $("#insert1").click(function(){ var collegeId1=$("#collegeId1").val(); $.post('insert.php', {collegeId: colle ...

Streaming live audio through socket io. Need help troubleshooting ALSA shutdown issue

Looking to develop a live audio streaming service using socket.io and ionic 4. On the client side, utilizing cordova-plugin-audioinput and ng-socket-io for Angular. For the server, employing standard npm packages. Node version: 10.16.0 ...

What is the best way to extract specific information from an API using AJAX and Axios?

I can't figure out what I'm doing wrong when trying to access a URL from an API using Axios. For instance, I'm attempting to select the following URL: "https://media4.giphy.com/media/yy6hXyy2DsM5W/giphy-downsized.gif?cid=482277c2s3j1s8uell6v ...

"When an input event occurs in Vue.js, a div is displayed and the text in the

I am facing a challenge where I need a div to appear below a text input once the user starts typing in the input field: https://jsfiddle.net/chadcf/3vjn71ap/ The structure is as follows: <div id="app"> <input id="foo" name="foo" ...