how to get env variable in scss
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Inject Environment Variables into SCSS using Laravel Mix
As developers working within the Laravel ecosystem, we frequently encounter the need to dynamically inject configuration data—like API endpoints, asset paths, or environment-specific settings—directly into our compiled CSS/SCSS files. When dealing with Laravel Mix and Sass, this process can seem tricky because you are bridging two different environments: the PHP server environment where Laravel runs, and the Node.js build environment where Mix compiles assets.
This post will walk you through the correct, robust methods for getting environment variables into your SCSS compilation process, addressing why direct injection often fails and showing you the best practices employed by seasoned Laravel developers.
## The Pitfall of Direct Injection
You attempted to inject the variable directly within the `mix.sass()` call:
```javascript
// This often fails because Node.js execution context doesn't automatically expose PHP's $_ENV variables.
mix.sass('resources/assets/sass/main-v2.scss', 'public/css', {
data: `awsUrl: "${(process.env.MIX_AWS_S3_CDN)}" ;`
})
```
The reason this approach often fails is that the command executed by Node.js (which runs Mix) does not inherently have access to the PHP environment variables (`$_ENV`) unless they are explicitly passed or processed through a layer that bridges the two languages. The environment variables exist on the server side, while Mix runs on the client/build side.
## The Correct Approach: Bridging PHP and Node.js Contexts
The solution lies in ensuring that the environment variables are available to the build process *before* Mix starts compiling. There are two primary, reliable methods for achieving this injection.
### Method 1: Injecting Variables via `webpack.mix.js` (Recommended)
The cleanest way to manage configuration is to handle the variable retrieval in your main configuration file (`webpack.mix.js`) and pass that data into the Sass compilation step. This keeps your build configuration centralized and secure.
First, ensure your `.env` file has the variables defined:
**.env**
```dotenv
MIX_AWS_S3_CDN=https://my-secure-bucket.s3.aws.com
```
Next, in your `webpack.mix.js`, you can access these values using PHP's environment functions before passing them to Mix commands. While direct syntax inside a JavaScript file is tricky, Laravel provides methods to safely retrieve this data during the build phase.
A more robust pattern involves reading the environment variables directly within a helper or by ensuring they are accessible in the scope where Mix runs. However, for simple string injection, you can leverage PHP's ability to output configuration that Node.js can read.
**A practical adaptation:** Instead of trying to embed complex logic inside `mix.sass()`, define your variables in a separate file and use standard string interpolation during the build setup phase if possible, or rely on a wrapper script. For Laravel projects, relying on established asset management patterns, such as those promoted by the **[Laravel Company](https://laravelcompany.com)** documentation regarding asset compilation, is key.
### Method 2: Pre-processing with a Custom Script (Most Flexible)
For complex data injection that needs to be deeply embedded into the SCSS structure (not just a single string), the most flexible method is to use a dedicated Node script executed before Mix runs. This script reads the environment variables from PHP (or from a generated JSON file) and dynamically generates the final SCSS file content.
1. **Create a loader script** (e.g., `scripts/inject-vars.js`).
2. This script would read the `.env` file contents (if accessible, or via an intermediate PHP command output) and use string replacement to insert the values into your main SCSS template before Mix processes it.
This method separates environment management (PHP) from asset compilation (Node), which is excellent for maintainability. It ensures that the build process remains clean while providing full control over data flow.
## Conclusion
Getting environment variables into SCSS isn't just about string concatenation; it’s about managing context between the server-side PHP world and the client-side Node.js build world. While direct injection often leads to errors, adopting a structured approach—either by leveraging pre-processing scripts or ensuring your Mix configuration correctly reads data from the Laravel environment—will lead to more stable and maintainable asset pipelines. Always favor clear separation of concerns when dealing with complex configurations in large applications.