• Stars
    star
    170
  • Rank 223,357 (Top 5 %)
  • Language
    JavaScript
  • Created almost 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Inject env vars for your Nuxt app at runtime

nuxt-env

nuxt-env inject env vars for your Nuxt app at runtime

CircleCI


🚨🛑 WARNING 🛑🚨

Runtime Config has been released in Nuxt.js and solves the problem this package was solving, so this package has been is deprecated and will not be receiving any further updates (There is some discussion in this issue in the Nuxt.js repo).


Why

Nuxt currently provides a very handy way of injecting environment variables which uses webpack substitution to inject your env vars at build time. This works most of the time, but if your build process is environment-agnostic (e.g. if you build a Docker image on CI and use the same image for staging and production), you end up with a result which has the environment baked into it (meaning that in our example, the docker image would be coupled to the environment it was built in).

This module allows you to read environment variables server side—at runtime—and inject them into your app, meaning your Nuxt bundle is decoupled from your environment variables.

⚠️ WARNING: As with the config.env option in Nuxt config, environment variables used in nuxt-env are exposed client side, so if you store secrets use the secret config option. Read more below. ⚠️

Usage

nuxt-env injects your environment variables into your Nuxt app using this.$env.

N.B. If currently use Nuxt's config.env option, fear not—nuxt-env includes those env vars in the $env object.

Get Setup

  1. Install the dependency:
yarn add nuxt-env
  1. Add to your nuxt.config.js and configure:
// nuxt.config.js

// Tell nuxt-env which env vars you want to inject
modules: [
  'other-nuxt-module',
  ['nuxt-env', {
    keys: [
      'TEST_ENV_VAR', // Basic usage—equivalent of { key: 'TEST_ENV_VAR' }
      { key: 'OTHER_ENV_VAR', default: 'defaultValue' } // Specify a default value
      { key: 'THIRD_ENV_VAR', secret: true } // Only inject the var server side
      { key: 'ANOTHER_ENV_VAR', name: 'MY_ENV_VAR' } // Rename the variable
    ]
  }]
]

Options

Env vars can be injected in a basic way, just by specifying a string in the keys option. When the provided var is an object, it can have the following attributes:

key

required

The name of the environment variable by which it can be accessed in process.env

default

A default value for the env var in case it's not present in process.env.

secret

default: false

When true, this key will only be present server side.

name

Change the name of the env var that gets injected. e.g.: { key: 'API_URL', name: 'API_ENDPOINT' } will read process.env.API_URL and add it as $env.API_ENDPOINT

Use in your application

  • Use this.$env in your components:
// any-component.vue

export default {
  computed: {
    testValue () { return this.$env.TEST_VALUE }
  }
}
  • and in your Nuxt context
// any-component.vue

export default {
  asyncData ({ app }) {
    console.log(app.$env.TEST_VALUE)
  }
}
  • and in your store:
// store/index.js

export const mutations = {
  storeEnv (commit) {
    const val = this.$env.TEST_VALUE
    commit('testValue', val)
  }
}

Develop

# First fork the repo
git clone [email protected]:your-username/nuxt-env.git
cd nuxt-env
npm i
npm run test

# To use while developing other apps:
npm link
cd ../my-other-app
npm link "nuxt-env"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/samtgarson/nuxt-env. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Thanks

License

The module is available as open source under the terms of the MIT License.

More Repositories

1

micro-starter

[Deprecated] 🔷 Basic (opinionated) starter kit for a micro app with webpack build
JavaScript
141
star
2

vueport

[Deprecated] Single file components for Rails with Vue JS and Webpack
Ruby
139
star
3

nuxt-csrf

[WIP] CSRF protection for your Nuxt app
JavaScript
12
star
4

vueport-example

An example Rails application using Vueport gem
Ruby
11
star
5

rsms-email

A SwiftUI challenge to build an email client I saw on Twitter
Swift
9
star
6

iterm-snazzy

Port of the Snazzy Hyperterm theme to Iterm 2
8
star
7

asdf-1password

1password plugin for asdf version manager
Shell
7
star
8

dotfiles

📦 new machines the way I like them
Lua
5
star
9

pat

📮 Postman on the command line
TypeScript
3
star
10

vuex-scroll

Keep vuex state updated with scroll stats
JavaScript
3
star
11

lunar-and-flo-api

Backend and API to serve mobile app for Lunar & Flo period tracker
Ruby
2
star
12

dobble

TypeScript
1
star
13

tactile-controller

An experimental app to turn your iPhone into a multi-faceted input device
Swift
1
star
14

do-spinner-upper

A small utility to save and recreate Digital Ocean droplets
Shell
1
star
15

portfolio-2

Latest attempt at a portfolio I'm happy with
Vue
1
star
16

books-about-food

TypeScript
1
star
17

hooks

Webhooks for personal projects
TypeScript
1
star
18

celesta

Keep an eye on your practice
JavaScript
1
star
19

typescript-microservice

A quick template repo for tiny typescript microservices
TypeScript
1
star
20

vue-renderer

Microservice to render your Vue templates
JavaScript
1
star
21

github-deploy-bot

A bot to post deployment information to Pull Requests
Ruby
1
star
22

digest-delivery

Get your articles and feeds delivered to your Kindle.
TypeScript
1
star
23

now-latest

Get the latest deployment for your now.sh app
JavaScript
1
star