Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Apps | Demo | Docs | Install
  1. Cloudron Forum
  2. Directus
  3. Unable to Access Environment Variables in Vue.js App on Cloudron

Unable to Access Environment Variables in Vue.js App on Cloudron

Scheduled Pinned Locked Moved Directus
4 Posts 2 Posters 673 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    webthingz
    wrote on last edited by
    #1

    Hello,

    I'm currently developing a Vue.js application and deploying it on Cloudron. I'm trying to access an environment variable (VUE_APP_DIRECTUS_TOKEN) in my Vue.js component, but it always returns undefined.

    Here's the relevant part of my code:

    methods: {
      async register() {
        try {
          await axios.post(
            'https://directus.example.com',
            {
              email: this.email,
              password: this.password,
              status: 'active'
            },
            {
              headers: {
                Authorization: `Bearer ${import.meta.env.VUE_APP_DIRECTUS_TOKEN}`
              }
            }
          )
          alert('Registration successful')
        } catch (error) {
          alert('Registration failed: ' + error.message)
        }
      }
    }
    

    I've tried setting the environment variable in a .env file in the root of my project, and also in an env.sh file in the /app/data/ directory as suggested by Cloudron's documentation. I've also tried using the export keyword. However, none of these methods seem to work.

    Here's how I've set the environment variable:

    export VUE_APP_DIRECTUS_TOKEN=my_token_here
    

    Sure, here's a draft for your forum question:


    Title: Unable to Access Environment Variables in Vue.js App on Cloudron

    Body:

    Hello,

    I'm currently developing a Vue.js application and deploying it on Cloudron. I'm trying to access an environment variable (VUE_APP_DIRECTUS_TOKEN) in my Vue.js component, but it always returns undefined.

    Here's the relevant part of my code:

    methods: {
      async register() {
        try {
          await axios.post(
            'https://wij.metaalkathedraal.nl/users',
            {
              email: this.email,
              password: this.password,
              status: 'active'
            },
            {
              headers: {
                Authorization: `Bearer ${import.meta.env.VUE_APP_DIRECTUS_TOKEN}`
              }
            }
          )
          alert('Registration successful')
        } catch (error) {
          alert('Registration failed: ' + error.message)
        }
      }
    }
    

    I've tried setting the environment variable in a .env file in the root of my project, and also in an env.sh file in the /app/data/ directory as suggested by Cloudron's documentation. I've also tried using the export keyword. However, none of these methods seem to work.

    Here's how I've set the environment variable:

    export VUE_APP_DIRECTUS_TOKEN=your_token_here
    

    I've made sure to restart the app after making changes to the environment variables.

    I'm able to hardcode the token directly into the code and it works, so the issue seems to be with loading the environment variable.

    Does anyone have any suggestions on how to correctly set and access environment variables in a Vue.js app on Cloudron?

    Thank you in advance for your help.

    1 Reply Last reply
    1
    • nebulonN Offline
      nebulonN Offline
      nebulon
      Staff
      wrote on last edited by
      #2

      I guess this is more like a generic question not so much related to Directus. So environment variables are not exposed automatically from a .env file, you would need to source /app/data/.env in your start.sh script which should be the entry point CMD [ "/app/code/start.sh" ] in your Dockerfile.

      You can find more info at https://docs.cloudron.io/packaging/cheat-sheet/ with links to existing packages for inspiration.

      W 1 Reply Last reply
      0
      • nebulonN nebulon

        I guess this is more like a generic question not so much related to Directus. So environment variables are not exposed automatically from a .env file, you would need to source /app/data/.env in your start.sh script which should be the entry point CMD [ "/app/code/start.sh" ] in your Dockerfile.

        You can find more info at https://docs.cloudron.io/packaging/cheat-sheet/ with links to existing packages for inspiration.

        W Offline
        W Offline
        webthingz
        wrote on last edited by
        #3

        @nebulon Hi Nebulon, sorry for my late response. Busy week!

        When I look at the start.sh file I see the following lines:

        if [[ ! -f /app/data/env.sh ]]; then
            sudo -u cloudron cat > /app/data/env.sh <<EOF
        export KEY="$(uuid -v4)"
        export SECRET="$(pwgen -1 32)"
        EOF
        fi
        
        cp /app/data/env.sh /run/directus/env.sh
        [[ -f /app/data/env ]] && cat /app/data/env >> /run/directus/env.sh
        
        source /run/directus/env.sh
        

        So it looks like it's allready there?

        1 Reply Last reply
        0
        • nebulonN Offline
          nebulonN Offline
          nebulon
          Staff
          wrote on last edited by
          #4

          It is only created within the directus app instance. Different apps run isolated from one another on Cloudron. So if I understand you correctly, you are trying to build your own custom app next to directus? If so you need to set the source your own /app/data/env.sh with the export of the env var.

          1 Reply Last reply
          1
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Bookmarks
          • Search