Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sasafister's avatar

How to get .env variable in Nova package

I have package that is located in vendor folder. Package in Nova Tool and there is component. How can I fetch .env file in that /vendor/package/component? It works only if I put .env into vendor/package folder, but I want to put data in .env that is located in root of project. Anyone has an idea?

0 likes
2 replies
FutureFreak's avatar

I'm also looking for an answer to this.

I've created a custom component/tool and I'm trying to pull in the .env variables into a vue component using MIX_STRIPE_KEY.

This works if I put in a 'local' .env file into the root of the component folder, but I don't want to do this - as I want to have a single place for the project that holds the STRIPE_KEY, so I want it to obtain the variable from the root .env file.

dualklip's avatar

I know it's been a long time since this thread was last posted, but I'm leaving this info here in case it might be of interest to anyone.

Due to Nova custom component can't access to the main .env file you can inject whatever you want on the component with Nova::provideToScript() function using it from your ServiceProvider.php file:

 public function boot(): void
    {
        $this->app->booted(function () {
            $this->routes();
        });

        Nova::serving(function (ServingNova $event) {
            Nova::script('my-custom-component', __DIR__.'/../dist/js/card.js');
            Nova::style('my-custom-component', __DIR__.'/../dist/css/card.css');
            Nova::provideToScript([
                'tool' => [
                    'baseUrl' => config('app.url'),
                ],
            ]);
        });
    }

Then you can access to this info from the Vue component doing this:

Nova.config('tool').baseUrl

Please or to participate in this conversation.