How to access the environment parameter from ember-cli-build.js

When you do:

ember build --environment="production"

The environment parameter is made available in config/environment.js:

module.exports = function(environment) {
  ...
};

My app has requirements which call for an environment dependent prepend (in ember-cli-build.js):

var STATIC_URL = "TODO";  // This depends on the deploy "environment" parameter

module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
    fingerprint: {
      enabled: true,
      prepend: STATIC_URL,
    },
  });
  return app.toTree();
};

How can I access the environment parameter from ember-cli-build.js?

EDIT

My versions:

» ember --version
version: 1.13.13
node: 0.12.2
npm: 2.14.10
os: linux x64

In our Brocfile.js (I guess yours is called ember-cli-build.js?) we are doing something like this:

var EmberApp = require('ember-cli/broccoli/ember-app');
var environment = process.env.EMBER_ENV || 'development';
var config = require('./config/environment')(environment);

var app = new EmberApp(/* configuration for the app... */ );
module.exports = app.toTree();

The line where we're assigning to the environment variable is how you get which environment you're in. We use the EMBER_ENV command line variable but you can use something different. Basically in all our code we run ember like this:

EMBER_ENV=production ember-cli start
EMBER_ENV=test ember-cli test

# the next lines use the same 'development' environment
EMBER_ENV=development ember-cli start
ember-cli start

Access ENV at build time (as opposed to runtime), Access the build environment parameter (“development” / “production”) from ember-cli-build.js . This should be already available, since the  Ember CLI ships with support for managing your application's environment. The runtime environment for the application is defined in config/environment.js.Here an object ENV is built for each of the three Ember CLI-supported build modes: development, test, and production.

I'm using Ember 2.5. To access the environment parameter from ember-cli-build.js, use process.env.EMBER_ENV. Here's my ember-cli-build.js:

let EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  let app = getApp(defaults, process.env.EMBER_ENV);

  // Use `app.import` to add additional libraries to the generated
  // output files.

  return app.toTree();
};

function getApp(defaults, environment) {
  switch(environment) {

    case "production":
      return new EmberApp(defaults, {
        fingerprint: {
          enabled: true
        },
      });

    default:
      return new EmberApp(defaults, {
        fingerprint: {
          enabled: false
        },
      });     

  }
}

How to just build for an environment other than production, is it possible to just build an app for another environment taking into account variables and secrets by environment without using ember-cli-deploy  The assets manifest is located in the ember-cli-build.js file in your project root (not the default ember-cli-build.js). To add an asset specify the dependency in yourember-cli-build.js before calling app.toTree(). You can only import assets that are within the bower_components or vendor directories. The following example scenarios illustrate

EDIT: I just recognized that you need environment already in ember-cli-build.js not only in app.js, so this answer might not work. I'll leave it posted anyway, may be it'll help!

my configuration is a bit different, but the including of enviroment is the same:

// app.js - I stripped some unrelated stuff
import Ember from 'ember';
import Resolver from 'ember/resolver';

import ENV from 'my-appname/config/environment';

var App;


App = Ember.Application.extend({
  fingerprint: {
     enabled: true,
     prepend: ENV.STATIC_URL,
  },
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  Resolver: Resolver,
});

export default App;

Now you can change STATIC_URL in enviroment.js depending on the enviroment you get passed to:

// config/enviroment.js
module.exports = function(environment) {
    var ENV;
    if(environment==='production') {
        ENV.STATIC_URL='foo';
    }
    return ENV;
}

Note that config/environment lives under your dasherized appname.

ember-cli-dotenv, Expose .env variables in Ember applications. options from ember-cli-build.js to the function declared within config/dotenv.js Then, you can access the environment variables anywhere in your app like you usually would. Hi, I ran into an issue on a project where ember build --environment and ember build --production resulted in different builds.. Specifically, the codebase was making use of __container__ calls, which were fine in the staging build but caused reference errors in the production build.

EmberCLI ‐ User guide, This will give you access to the ember command-line runner. ember-cli-build.js, Contains the build specification for Broccoli. Similarly, you can give any name to the variable into which you import a module when doing so When the app runs in /tests it runs in the development environment, not the test environment. Create an environment in the SSIS catalog, along with a variable ; Link the environment to the project and the variable with the parameter Execute a package using the environment ; SSIS Project and Package Set-up with Parameters. The easiest way to work with environment variables is first to use parameters in your packages.

How to access environment variables(config/environment) from , I need this because a third party requires a variable to be declared on window object. to host and review code, manage projects, and build software together. I could not find any info about this on the ember-cli website. As parameter for UseUrls I want to give something I read from the Configuration, So, I need to to do the ConfigurationBuilder stuff in static main rather than startup class. That makes that the added value of startup class is reduced and I should better do the ConfigureServices and Configure directly in the WebHostBuilder.

Load environment variables after build process. · Issue #2827 , variable (e.g. "environment": production) is via the .ember-cli file and. the index file changes with config changes the js and css files should  Once Advanced System Settings is open, click on the Advanced tab, then look on the bottom right side for the Environment Variables button you will find on the lower right-hand side. Click Environment Variables. Next, to create a new environment variable, just click New.

Comments
  • Are you using Broccoli? where is the above code block located, what's the filename?
  • @omouse: I am using ember-cli, which is using broccoli (as listed in the packages.json). I have added the versions of ember-cli et al. The code location is already specified in the question, but I'll repeat. From the ember project top dir: config/environment.js and ember-cli-build.js
  • Thanks. Interesting, but maybe duplication of functionality. The ember executable already accepts a --environment parameter, so setting up and EMBER_ENV environment variable looks like redundant information.
  • And yes, my "Brocfile" is called now (ember-cli has recently renamed things) ember-cli-build.js
  • @gonvaled you may want to insert some console.logs to see if the right environment is being set (and yes EMBER_ENV is unique to our codebase, check out the ember-cli user guide on environment)
  • Looks good but you are doing the fingerprint on app.js? That is not what the ember-cli guide recommends. Now I am confused ... I thought Ember.Application from app.js is creating an Ember application on the client side, and has nothing to do with asset compilation. On the other hand, new EmberApp from ember-cli-build.js is setting up the broccoli asset pipeline to generate a distributable asset tree. Fingerprinting involves asset processing, including processing of the index.html. Not sure how it is done in app.js?!
  • And you are doing import ENV from 'my-appname/config/environment'; from app.js. I do not know what version of ember-cli you are using, or the structure of your project, but from a vanilla ember-cli application (1.13.13) you do not need to prefix the app-name on the import (actually, that is ember-cli generated code for me)
  • I have tried and it is indeed not working: the index.html has no references to the right assets (with the prefix STATIC_URL). I would say fingerprinting is not being used when defining Ember.Application (as I expected). Could you clarify your setup?
  • yes, the prefix of app-name I only include to make that path absolute, because when including environment in a nested controller I ended up with import ENV from '../../../../../../config/environment.js'. So I just kept it absolute everywhere.
  • no, I don't use fingerprint at all, I just included it because of your example. I didn't know it's a predefined property...