Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to use Webpacker::Compiler.env #2794

Closed
petitkriket opened this issue Nov 28, 2020 · 4 comments
Closed

failed to use Webpacker::Compiler.env #2794

petitkriket opened this issue Nov 28, 2020 · 4 comments

Comments

@petitkriket
Copy link

petitkriket commented Nov 28, 2020

Hi,
Having an issue with webpacker initializer.
As pointed in the env doc I create an initializer to pass Rails application variables to a JS frontend, process.env.FRONTEND_API_KEY ends up empty.

It has already been talked about but left unsolved.

Am I doing something wrong or missing something ?

# in config/initializersd/webpacker.rb
Webpacker::Compiler.env['FRONTEND_API_KEY'] = Rails.application.credentials.dig(Rails.env.to_sym, :some_api_key)
# Webpacker.compiler.env returns a hash with the data
// in  application.js
console.log(process.env.FRONTEND_API_KEY)
// returns undefined

I'm having the same issue as @nozpheratu - setting variables with Webpacker::Compiler.env doesn't seem to make it into the actual javascript environment. Anyone have a suggestion? Did this break somewhere?

Originally posted by @bbugh in #691 (comment)

@petitkriket
Copy link
Author

petitkriket commented Nov 28, 2020

Ok digged google and found an article where @takeyuweb explain why it does not work on dev environment and how to circumvent the issue

# bin/webpack-dev-server
require_relative '../config/application'
Rails.application.initialize!

It works on production without doing anything and just fixes the development env issue

Should it be addressed ?

@wasifhossain
Copy link

adding the following

# bin/webpack-dev-server
require_relative '../config/environment'

might serve as a shortcut for:

Ok digged google and found an article where @takeyuweb explain why it does not work on dev environment and how to circumvent the issue

# bin/webpack-dev-server
require_relative '../config/application'
Rails.application.initialize!

It works on production without doing anything and just fixes the development env issue

Should it be addressed ?

@psemehen
Copy link

this was really helpful 👍

@wasifhossain
Copy link

the following link should work for the env doc shared in the OP:

https://github.com/rails/webpacker/blob/5-x-stable/docs/env.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants