Why does webpacker use both the RAILS_ENV and NODE_ENV values?

Why does the rails/webpacker gem depend on both the RAILS_ENV and NODE_ENV values?

Summary

  1. Both the JS and Ruby code of rails/webpacker uses the RAILS_ENV to find the applicable section of the config/webpack YAML file. This YAML file is the glue between the JS code that configures webpack and the Rails view helpers.
  2. The JS code that creates the webpack configuration object uses the NODE_ENV to find the default configuration as well as the customizable configuration in the config/webpack directory, such as using config/webpack/production.js for NODE_ENV production.

Details

The Ruby code of rails/webpacker’s view helpers use the RAILS_ENV to get values out of this config/webpacker.yml to configure the view helpers to use the right directory and other aspects that configure the Rails server and what is sent to the browsers in the HTML sent.

Here’s where webpacker picks the Rails.env for the view helpers.

lib/webpacker/env.rb:21 grabs the RAILS_ENV

rails/webpacker loads the YML file for Ruby view helpers here:

rails/webpacker webpack configuration, if you’re using that

You don’t have to use the rails/webpacker configuration of the webpack config object. However, most Rails apps will use that.

The output webpack configuration object uses both the RAILS_ENV and the NODE_ENV.

The package/env.js file defines the exports railsEnv and nodeEnv.

Config setting values from config/wepbacker.yml are the railsEnv values:

NODE_ENV is used to get the default configuration and the override file