vincent15000's avatar

Laravel / VueJS / Sail / frontend environment variables

Hello,

I read the documentation and I see that it's possible to access environment variables like this.

https://laravel.com/docs/11.x/vite#environment-variables

// .env
VITE_API_URL=http://localhost:8000
// JS
import.meta.env.VITE_API_URL

But it doesn't work and I get an error in the console.

TypeError: Cannot read properties of undefined (reading 'env')

So import.meta is null.

Why ?

Can you help me ?

Thanks a lot.

V

0 likes
3 replies
LaryAI's avatar
Level 58

Hello V,

It seems like you're encountering an issue where import.meta is undefined, which typically suggests that the script where you're trying to use import.meta.env is not being processed as a module. This is a common issue when the script tag lacks type="module" or the project configuration does not treat the file as a module.

Here are the steps to resolve this issue:

  1. Ensure Your Script is a Module: Make sure that your JavaScript file is treated as a module. If you are directly including it in an HTML file, you should specify type="module" in your script tag. Here’s how you can do it:

    <script type="module" src="path/to/your/script.js"></script>
    
  2. Check Your Project Configuration: If you are using a build tool like Vite (which you are, as per your .env setup), ensure that your configuration supports module syntax and that you are importing and exporting your scripts correctly.

  3. Accessing Environment Variables in Vite: With Vite, environment variables prefixed with VITE_ in your .env file are exposed to your code through import.meta.env. However, this will only work if the files are correctly processed as modules by Vite. Ensure that your Vite configuration is correct and that you are running your project using Vite's development server or build command.

    Here’s a basic example of how to access the environment variable in a Vue.js component:

    <script type="module">
    export default {
      mounted() {
        console.log(import.meta.env.VITE_API_URL);
      }
    }
    </script>
    
  4. Debugging: If you continue to face issues, try logging import.meta to the console to see what it contains. If it's still undefined, there might be a configuration issue with how your project is set up with Vite.

    console.log(import.meta); // Check what this outputs
    
  5. Consult Documentation and Community: Double-check the Vite documentation regarding environment variables and modules. If the problem persists, consider seeking additional help from community forums or the Vite GitHub issues page.

By following these steps, you should be able to resolve the issue with accessing environment variables in your Laravel/VueJS application using Vite and Sail.

gych's avatar

First try this in app.js to see if that works.

console.log(import.meta.env.VITE_API_URL);

Only times it didn't work for me was with js files run by nodejs, then I use dotenv instead to import the env variable.

1 like
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

To work, the JS script has to be of type module.

I have solved this issue by retrieving the environment datas from inside another JS file and then I import the properties when needed.

Please or to participate in this conversation.