# @moxy/next-env

> Next.js plugin to pass environment variables to Next's configuration

Latest version **2.0.1** (published 2019-10-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @moxy/next-env
pnpm add @moxy/next-env
yarn add @moxy/next-env
bun add @moxy/next-env
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2019-10-22 |
| First published | 2019-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Ivo Lima Silva |
| Maintainers | acostalima, afonsovreis, dominguesgm, filipediasf, fsdiogo, hugomrdias, ivolimasilva, marcooliveira, moxyhq, nlfonseca, paulobmarcos, pedromiguelss, raising, ricardo-silva91, ritazoliveira, ruimonteiro, ruipneves, satazor, threequartersjohn, vascosantos |
| Keywords | next, nextjs, plugins, environment, env, dotenv, runtime, buildtime |

## Links

- npm: https://www.npmjs.com/package/@moxy/next-env
- Repository: https://github.com/moxystudio/next-env
- Homepage: https://github.com/moxystudio/next-env#readme
- Issues: https://github.com/moxystudio/next-env/issues
- npm.io page: https://npm.io/package/@moxy/next-env

## 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

- 2.0.1 (latest) — 2019-10-22
- 2.0.0 — 2019-10-03

## README

# next-env

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]

[npm-url]:https://npmjs.org/package/@moxy/next-env
[downloads-image]:https://img.shields.io/npm/dm/@moxy/next-env.svg
[npm-image]:https://img.shields.io/npm/v/@moxy/next-env.svg
[travis-url]:https://travis-ci.org/moxystudio/next-env
[travis-image]:http://img.shields.io/travis/moxystudio/next-env/master.svg
[codecov-url]:https://codecov.io/gh/moxystudio/next-env
[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/next-env/master.svg
[david-dm-url]:https://david-dm.org/moxystudio/next-env
[david-dm-image]:https://img.shields.io/david/moxystudio/next-env.svg
[david-dm-dev-url]:https://david-dm.org/moxystudio/next-env?type=dev
[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/next-env.svg

Next.js plugin to pass environment variables to Next.js' configuration.

By default, Next.js does not make available `process.env` to the client side. The available solution for this problem is to set a config in `next.config.js` to pass values which will be available in either server and client or just server.

The way to do it is by passing values to the `publicRuntimeConfig`, `serverRuntimeConfig` and `env` properties of Next.js' config.

*More info: https://nextjs.org/docs#build-time-configuration*

This plugin does that automatically for all environment variables starting with a certain prefix.

## Installation

```sh
$ npm i --save @moxy/next-env
```

## Usage

```js
// next.config.js
const withEnv = require('@moxy/next-env');

module.exports = withEnv({ ...options })({ ...nextConfig });
```

Multiple configurations can be combined together with function composition. For example:

```js
// next.config.js
const withCSS = require('@zeit/next-css');
const withEnv = require('@moxy/next-env');

module.exports = withCSS(
    withEnv({
        removePrefixes: true,
    })({
        cssModules: true, // this options will be passed to withCSS plugin through nextConfig
    }),
);
```

Application usage:

```js
// next.config.js
const withEnv = require('@moxy/next-env');

module.exports = withEnv({ removePrefixes: true })({ ...nextConfig });
```

```sh
# environment variables definition
NEXT_PUBLIC_FOO="bar"
NEXT_SERVER_FOO="foo"
NEXT_BUILD_BAR="compile-me-please"
```

```js
// app.js
const { publicRuntimeConfig } = getConfig():

const x = publicRuntimeConfig.FOO; // 'bar'
const y = 'compile-me-please' // original code was `const y = process.env.BAR;


// server.js
const { serverRuntimeConfig } = getConfig():

const x = serverRuntimeConfig.FOO; // 'foo'
```

### Available options

| Option | Description | Type | Default |
|---|--------------------------------------------------------------------|---------|-----------|
| publicPrefix | Prefix of variables to lookup and then pass to publicRuntimeConfig | String | `NEXT_PUBLIC_` |
| serverPrefix | Prefix of variables to lookup and then pass to serverRuntimeConfig | String | `NEXT_SERVER_` |
| buildPrefix  | Prefix of variables to lookup and then pass to env to be injected in compile-time | String | `NEXT_BUILD_` |
| removePrefixes | Option to remove prefix when passing variables to config | Boolean | `false` |

## Tests

Any parameter passed to the `test` command, is passed down to Jest.

```sh
$ npm t
$ npm t -- --watch # during development
```

## License

Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).

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