ember-cli-dotenv: This addon is incompatible with ember-cli@2.16.0-beta.2+

Hi.

Suddenly After updating to ember-cli@2.16.0-beta.2, ember-cli-dotenv stopped passing environment variables. In config/environment.js, process.env does not contain env vars from the dot-env file.

This happened to existing commits that have been working previously. My app uses Yarn with a proper lockfile, so I think it may be something with my OS.

Please help me debug this and figure out the reason.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 47 (6 by maintainers)

Most upvoted comments

I have a fork going that moves the configuration out of ember-cli-build, which resolves this issue for now. https://github.com/jasonmit/ember-cli-dotenv

Readme diff summarizes the changes: https://github.com/jasonmit/ember-cli-dotenv/commit/b055555270217bec671f97d78f90b8d6181641cd#diff-04c6e90faac2675aa89e2176d2eec7d8

@fivetanley feel free to fold these changes in if you want. Otherwise I’ll continue to maintain the fork for others.

@SergeAstapov @jasonmit thanks for the awesome work through this thread - was tracing a few subtle bugs for a while, finally realized it was dotenv, and stumbled on this thread. After upgrading and migrating, everything works again.

Appreciate all the hard work!

@SergeAstapov @jasonmit sorry for the delay, you both have publish rights on npm. thanks so much for your help

Thank you @fivetanley! Version 2.0 has been pushed to npm! 🎉

Thank you all for the help, feedback and patience!

Has anyone gotten this updated version to work properly with ember-cli-deploy? I keep getting development variables rather than my other environments.

@oxodesign changes are in master and will be published soon.

@jasonmit I added you and @SergeAstapov as collaborators on this repo. a 2.x release sounds great. It sounds like you have solved the issues on your fork? Feel free to merge those in.

@jeanduplessis for some reason ember build and ember s works differently:

  1. define a variable SOME_VAR_NAME in .env like
SOME_VAR_NAME=jln4i7cl
  1. install addon from SergeAstapov/ember-cli-dotenv#update-ember-cli
  2. change config/environment.js like
ENV['APP_VAR_NAME'] = process.env.SOME_VAR_NAME;
console.log(process.env.SOME_VAR_NAME);

After these steps do ember build and you should see process.env.SOME_VAR_NAME variable value in meta tag in the dist/index.html file:

<meta name="dummy/config/environment" content="{ ... "APP_VAR_NAME": "jln4i7cl" ... }" />

And CLI console output would be:

undefined
jln4i7cl
jln4i7cl

If you then would do ember s CLI console output would be:

undefined
undefined
Proxying to https://example.com
Livereload server on http://localhost:35129
jln4i7cl

and you would not see process.env.SOME_VAR_NAME variable value in meta tag in the dist/index.html file until you do some change and app is re-build.

What is current addon behavior:

read .env file in config() hook of addon. ember-cli <= 2.15 invoked config/environment.js file multiple times. Even after config() hook of this addon. So as a result process.env variables from .env file would became available next time config/environment.js is included. Change introduced in ember-cli@2.16 reads config/environment.js once and prior to config() hook of any addon.

What was implemented in SergeAstapov/ember-cli-dotenv#update-ember-cli

read .env file in included() hook of addon This change still does not seem to solve the issue completely.

What could be done else:

read .env file in init() hook of addon. Problem would be that ember-cli-build.js is not invoked yet and we do not have access to neither dotEnv configuration options within ember-cli-build.js nor we do not know what is the environment name build is running in (like development, test or production). This would allow us to read .env file in project root prior to ember-cli-build.js or config/environment.js file is included and executed so process.env would be available in config/environment.js.

But on the other hand this change would prevent anyone from using custom .env file path or have per environment .env files (which does not considered as best practice at all, see dotenv README.md).

Simple solution

Do not use process.env within config/environment.js You can continue to use an addon with clientAllowedKeys as described in https://github.com/fivetanley/ember-cli-dotenv#what-is-ember-cli-dotenv. This would work in simple use cases but would not allow you to put some variable from .env into nested object.

I can’t see any better solution then what I have already implemented in SergeAstapov/ember-cli-dotenv#update-ember-cli. If anyone has any suggestions - would be glad to help.

Would be great to hear from @fivetanley who is original addon author.

@SergeAstapov while I can confirm that the values from our .env file gets added to environment config, we were previously using process.env.KEY_NAME in environment.js to set variables in a structure that other addons might expect.

For example ember-intercom-api expects you to set the following:

ENV['ember-intercom-api'] = {
  appId: '[YOUR_APP_ID]'
};

and we were setting it as such:

ENV['ember-intercom-api'] = {
  appId: process.env.INTERCOM_APP_ID
};

Also we don’t use .env files on our production server so we rely on accessing the info via process.env.XXX so that it works both during local development as well as on production.

@jeffrey008

  1. In terminal:

    npm uninstall ember-cli-dotenv
    npm install -D dotenv lodash
    
  2. In your ember-cli-build.js, do this:

    const dotenv = require ('dotenv')
    dotenv.config() // You can also provide the path to a specific dotenv file: dotenv.config({path: '.env.prod'})
    

    This will make env vars from your dotenv file available in the process.env.

  3. In your config/environment.js, do this:

    const _ = require 'lodash'
    
    const ENV = {
      envVars: _.pick(process.env, [
        'BACKEND_URL',
        'GITHUB_API_KEY',
      ])
    }
    
  4. Then in your app you can do this:

    import ENV from '<your-app-name>/config/environment'
    
    export default JSONAPIAdapter.extend({
      host: ENV.envVars.BACKEND_URL
    })