webpacker: assets:compile doesn't respect NODE_ENV
It seems that NODE_ENV=development bundle exec rails assets:precompile
ignores NODE_ENV
environment variable.
In order to confirm the problem, I added simple debug code to config/webpack/production.js
+if (process.env.NODE_ENV === 'production') {
+ throw 'NODE_ENV: ' + process.env.NODE_ENV;
+}
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
And here is the result.
/Users/mito/work/webpacker-test% NODE_ENV=development bundle exec rails assets:precompile
yarn install v1.5.1
[1/4] š Resolving packages...
success Already up-to-date.
āØ Done in 1.25s.
Webpacker is installed š š°
Using /Users/mito/work/webpacker-test/config/webpacker.yml file for setting up webpack paths
Compilingā¦
Compilation failed:
/Users/mito/work/webpacker-test/config/webpack/production.js:2
throw 'NODE_ENV: ' + process.env.NODE_ENV;
^
NODE_ENV: production
In spite of NODE_ENV=development
, process.env.NODE_ENV
is production
.
Probably, itās caused by this line https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L24
This is a repository which includes the reproducing code. https://github.com/y310/webpacker-test
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 38 (21 by maintainers)
Yeah, this is very misleading in the docs, if you run:
This will load
config/webpack/production.js
There is no way to
assets:precompile
against anything other thanconfig/webpack/production.js
because it is hard-coded to'production'
in https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L22I want to run
NODE_ENV=staging
and use that to point my bundle to a different API endpoint used in staging vs production. That is my use case, and it seems odd that assets:precompile does not honour NODE_ENV.@gauravtiwari - We have spent hours debugging the above bug, since it directly contradicts the Custom Rails Environments instructions for this gem, which state that NODE_ENV can force
assets:compile
to compile in development mode:If one has to use
assets:precompile
, is it possible to compile in development mode?Hey @jakeNiemiec, whatās the high-level rationale for restricting NODE_ENV to only production/development rather than respecting the NODE_ENV passed by the developer?
Iām not even getting to the part of running the tests because of this behavior!
bundle exec rails assets:precompile
crashes because initializers that shouldnāt be invoked are crashing due the problems described above. Same withbundle exec rails webpacker:precompile
.The only thing that works to even get into the testing phase is running
bin/webpacker
which allows us to bypass this entire mess.When our applications are running in production mode, they need to call specific endpoints to register themselves. Only in production mode, because for the other environments, we simply donāt care.
With this issue from webpacker, this causes our tests in CI to simply crash because either the endpoint they need to call is not available/reachable, or they donāt have the required environment variables available to make these calls.
Why? Because webpacker decides to load production.js and the production configuration because of this issue, something thatās not wanted at all when running explicitly in a testing environment.
So right now we have the following choices:
webpacker
whatsoever everif
statements in our production configuration files to detect whether weāre running under a CI environment and skip sectionsRight now weāre seriously going with option one if this is the behavior webpacker will be keeping.
@coding-bunny Iāve adjusted my process to have NODE_ENV to only ever be equal to ādevelopmentā or āproductionā, and to only be used for whether the bundle is in ādevelopment modeā, or for the case of prod or CI, the minified / bundled version
Iām inlining a client side API key with webpack / node-config (option 1 here) when I build my Docker image. The key must be different in QA / Prod environments.
But since NODE_ENV=qa isnāt respected, I get the prod key in both environments. It would be nice if webpacker recognized more environments than prod/dev.
@jakeNiemiec Also from the docs:
This isnāt true. Running
bundle exec rails assets:precompile
is going to ignoreNODE_ENV
and always use NODE_ENV=production.The āPlease note, NODE_ENV can either be set to production, development or testā and the example of specifying NODE_ENV=development and running the asset precompile lead you to believe specifying
NODE_ENV
does something. However, with the hardcoded production NODE_ENV in thewebpacker:compile
rake task, this isnāt the case.@gauravtiwari This is the output:
Local dev environment. Not CI.
Moving it to dependencies enabled
NODE_ENV=development ./bin/webpack
to work, and compiled the dev version š