# gulp-env-loader

> A gulp plugin for loading environment variables and replacing them in the contents of files.

Latest version **1.3.0** (published 2023-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install gulp-env-loader
pnpm add gulp-env-loader
yarn add gulp-env-loader
bun add gulp-env-loader
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2023-10-26 |
| First published | 2023-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 6 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mervin |
| Maintainers | mengqing723 |
| Keywords | gulp-env, gulp-env-loader, env loader, load env, dotenv, environment, variables, gulp-plugin |

## Links

- npm: https://www.npmjs.com/package/gulp-env-loader
- Repository: https://github.com/Meqn/gulp-env-loader
- Issues: https://github.com/Meqn/gulp-env-loader/issues
- npm.io page: https://npm.io/package/gulp-env-loader

## Dependencies (6)

- [dotenv](https://npm.io/package/dotenv.md) ^16.0.3
- [minimist](https://npm.io/package/minimist.md) ^1.2.8
- [through2](https://npm.io/package/through2.md) ^4.0.2
- [@types/node](https://npm.io/package/@types/node.md) *
- [plugin-error](https://npm.io/package/plugin-error.md) ^2.0.1
- [dotenv-expand](https://npm.io/package/dotenv-expand.md) ^10.0.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.3.0 (latest) — 2023-10-26
- 1.2.0 — 2023-10-25
- 1.1.3 — 2023-08-08
- 1.1.2 — 2023-08-08
- 1.1.1 — 2023-05-24
- 1.1.0 — 2023-04-29
- 1.0.0 — 2023-04-24

## README

# gulp-env-loader

[ [English](./README.md) | [中文](./README.zh_CN.md) ]

A gulp plugin for loading environment variables and replacing them in file contents.  
It can load environment variables from a specified configuration file or from the default `.env` file.

It uses `dotenv` to load additional environment variables from the following files in your environment directory, and it also statically replaces environment variables that appear in the file.

```
.env                # loaded in all cases
.env.local          # loaded in all cases, but ignored by git
.env.[mode]         # only loaded in specified mode
.env.[mode].local   # only loaded in specified mode, but ignored by git
```


> ignores `.*.local`, so you also need to add it to your project's `.gitignore` file:
```
# local env files
.env.local
.env.*.local
```



## Install

```
npm install -D gulp-env-loader
```

> `Node.js > 12`



## Usage

1. Create a `.env` file in the root directory of your project, or create different `.env` files for different environments, such as `.env.development`, `.env.production`, etc.

```yml
# .env configuration
APP_MODE="development"
APP_API_URL="http://test-api.com"
```

```yml
# .env.production configuration
APP_MODE="production"
APP_API_URL="https://api.com"
```

2. Create `gulpfile.js`
```js
const gulp = require('gulp')
const envInject = require('gulp-env-loader')() //!recommended to put at the beginning and execute immediately

// Output the configured environment variables
console.log('env', envInject.env)

gulp.task('build', function() {
  return gulp.src('./src/*.js', { sourcemaps: true })
    .pipe(envInject())
    .pipe(gulp.dest('./dist', { sourcemaps: '.' }))
})
```

3. You can add the runtime parameter `mode` at runtime, which will automatically load the corresponding environment variable configuration file.

```
gulp build --mode=production
```

4. Output results

Source file: `./src/api.js`
```js
export function userLogin(params) {
  return http.post(`${process.env.APP_API_URL}/user/login`, params)
}
```
Output file: `./dist/api.js`
```js
export function userLogin(params) {
  return http.post(`https://api.com/user/login`, params)
}
```



## API
```ts
require('gulp-env-loader')([config])
```

### config
An optional configuration object or configuration file path.  
If it is a string, it represents the configuration file path. If it is an object, it can contain the following properties:
- `path` - (`string`) Configuration file path, default is `.env`
- `mode` - (`string`) Environment mode name.
- `modekey` - (`string`) Environment mode key name, default is `mode`
- `ignoreProcessEnv` - (`boolean`) Turn off writing to `process.env`


### Return value
```js
envInject([option])
```
Creates a through2 stream for replacing environment variables in file contents.

- `isVar` - (`boolean`) Replaces environment variables with their corresponding string representations (Single quotation marks). Default is `true`.
- `env` - (`object`) Additional Environment Variables. 



## Thanks
* [dotenv](https://www.npmjs.com/package/dotenv)
* [dotenv-expand](https://www.npmjs.com/package/dotenv-expand)
* https://juejin.cn/post/6993224664705138702

---
_Source: https://npm.io/package/gulp-env-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
