Skip to content

failed to use Webpacker::Compiler.env #2794

@petitkriket

Description

@petitkriket

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions