ember-cli-dotenv
Expose .env variables in Ember applications
Last updated 5 months ago by jasonmit .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ember-cli-dotenv 
SYNC missed versions from official npm registry.

Build Status npm version Ember Observer Score

Ember CLI Dotenv

Installation

ember install ember-cli-dotenv

Upgrading to 2.0.0

  • ember install ember-cli-dotenv@^2.0.0
  • open config/dotenv.js and ember-cli-build.js
  • Move/convert the dotEnv application options from ember-cli-build.js to the function declared within config/dotenv.js

What is Ember CLI Dotenv?

This addon allows you to write environment variables in a .env file and expose them to your Ember app through the built-in config/environment.js that you can import in your app. For example, you might be building an app with Dropbox and don’t want to check your key into the repo. Put a .env file in the root of your repository:

DROPBOX_KEY=YOURKEYGOESHERE

Next, configure config/dotenv.js.

// config/dotenv.js
module.exports = function(env) {
  return {
    clientAllowedKeys: ['DROPBOX_KEY'],
    // Fail build when there is missing any of clientAllowedKeys environment variables.
    // By default false.
    failOnMissingKey: false
  };
};

All keys in .env are currently injected into node’s process.env. These will be available in your config/environment.js file:

// config/environment.js
module.exports = function(environment) {
  return {
    MY_OTHER_KEY: process.env.MY_OTHER_KEY
  };
};

You can then use the node process environment variables in other ember-cli-addons, such as express middleware or other servers/tasks.

Security: environment variables in config/environment.js are never filtered unlike using .env and clientAllowedKeys. Remember to use the environment variable passed into your config function to filter out secrets for production usage. Never include sensitive variables in clientAllowedKeys, as these will be exposed publicly via Ember's <meta name="app/config/environment"> tag.

Then, you can access the environment variables anywhere in your app like you usually would.

import ENV from "my-app/config/environment";

console.log(ENV.DROPBOX_KEY); // logs YOURKEYGOESHERE

You can read more about dotenv files on their dotenv repository.

All the work is done by ember-cli and dotenv. Thanks ember-cli team and dotenv authors and maintainers! Thanks Brandon Keepers for the original dotenv ruby implementation.

FastBoot support

This addon supports FastBoot via fastbootConfigTree build hook (requires ember-cli-fastboot 1.1.0 or higher). Use fastbootAllowedKeys configuration option to make variables available in FastBoot mode when Ember application is rendered server-side.

// ember-cli-build.js

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    dotEnv: {
      clientAllowedKeys: ['DROPBOX_KEY'],
      fastbootAllowedKeys: ['MY_API_SECRET', 'MY_OTHER_API_SECRET']
    }
  });

  return app.toTree();
};

Note: keys listed in fastbootAllowedKeys are not added to Ember's <meta name="app/config/environment"> tag and are not available to Ember application when it runs in browser.

Multiple Environments

Sometimes people may want to use different .env file than the one in project root. This can be configured as below:

// config/dotenv.js
module.exports = function(env) {
  return {
    clientAllowedKeys: ['DROPBOX_KEY'],
    path: './path/to/.env'
  };
};

In addition, you may also customize for different environments:

// config/dotenv.js
module.exports = function(env) {
  return {
    clientAllowedKeys: ['DROPBOX_KEY'],
    path: `./path/to/.env-${env}`
  };
};

With the above, if you run ember build --environment production, the file ./path/to/.env-production will be used instead.

Environment Specific Use

Sometimes people may want to only initiate the dotenv file in one or more environments (e.g. not in production) This can be configured as below:

// config/dotenv.js
module.exports = function(env) {
  return {
    enabled: env !== 'production' // default is TRUE for any environment
  };
};

When enabled is set to false, the dotenv protocol will not be used at all. This is great for quieting errors and side issues when deploying with a service like Heroku, where you can use environment variables set within the service and avoid storing a .env file in your repo.

Compatibility

This addon supports the Ember 2.x series, but it is also backwards-compatible down to Ember-CLI 0.1.2 and Ember 1.7.0.

For FastBoot support you need Ember 2.3 or higher (2.12.0 and higher is prefereable by ember-cli-fastboot) and ember-cli-fastboot 1.1.1 or higher.

Other Resources

Contributing

Installation

  • git clone https://github.com/fivetanley/ember-cli-dotenv.git
  • cd ember-cli-dotenv
  • npm install

Linting

  • npm run lint:js
  • npm run lint:js -- --fix

Running tests

  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in "watch mode"
  • npm test:ember – Runs the test suite against multiple Ember versions
  • npm test:node - Runs the test suite in test folder created with ember-cli-addon-tests

Running the dummy application

For more information on using ember-cli, visit https://ember-cli.com/.

Links

License

This project is licensed under the MIT License.

Current Tags

  • 3.1.0                                ...           latest (5 months ago)

25 Versions

  • 3.1.0                                ...           5 months ago
  • 3.0.1                                ...           a year ago
  • 2.2.3                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.2.2                                ...           a year ago
  • 2.2.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.2.0                                ...           5 years ago
  • 1.0.4                                ...           5 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.4.0                                ...           6 years ago
  • 0.3.4                                ...           6 years ago
  • 0.3.3                                ...           6 years ago
  • 0.3.2                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.1                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |