React-native-config: iOS Build Error - Please verify your .env file is correctly formatted.

Created on 11 Nov 2020  路  6Comments  路  Source: luggit/react-native-config

I git this error after installing pod and run from xcode

Invalid entry in .env file. Please verify your .env file is correctly formatted.

Most helpful comment

1.4.0 is free of this error. Downgrade to 1.4.0

All 6 comments

One issue at my setup has been, that ios/ReactNativeConfig/ReadDotEnv.rb doesn't accept anything else except key-value pairs. In my case, I had empty lines and comments in my .env file.

On quick solution is to remove those.

I patched the part of ios/ReactNativeConfig/ReadDotEnv.rb by replacing lines 41-52 with:

raw.split("\n").each_with_index.inject({}) do |h, line_pair|
  line, index = line_pair

  next h if line.nil? || line == '' || line == ' '
  next h unless line.match(comment_line_pattern).nil?

  m = line.match(dotenv_pattern)

  if m.nil?
    abort("Invalid entry in .env file. Please verify your .env file is correctly formatted (line: #{index + 1}, value: #{line}).")
  end

  key = m[:key]
  # Ensure string (in case of empty value) and escape any quotes present in the value.
  val = m[:val].to_s.gsub('"', '\"')
  h.merge(key => val)
end

It ignores comments and empty lines.

One issue at my setup has been, that ios/ReactNativeConfig/ReadDotEnv.rb doesn't accept anything else except key-value pairs. In my case, I had empty lines and comments in my .env file.

On quick solution is to remove those.

I patched the part of ios/ReactNativeConfig/ReadDotEnv.rb by replacing lines 41-52 with:

raw.split("\n").each_with_index.inject({}) do |h, line_pair|
  line, index = line_pair

  next h if line.nil? || line == '' || line == ' '
  next h unless line.match(comment_line_pattern).nil?

  m = line.match(dotenv_pattern)

  if m.nil?
    abort("Invalid entry in .env file. Please verify your .env file is correctly formatted (line: #{index + 1}, value: #{line}).")
  end

  key = m[:key]
  # Ensure string (in case of empty value) and escape any quotes present in the value.
  val = m[:val].to_s.gsub('"', '\"')
  h.merge(key => val)
end

It ignores comments and empty lines.

So can you show me comment_line_pattern

I also have comments in my .env files, so I updated it with this (same as @shllg's, but without the separate comment_line_pattern var):

raw.split("\n").each_with_index.inject({}) do |h, line_pair|
  line, index = line_pair

  next h if line.nil? || line == '' || line == ' '
  next h if line.match(/^\s*#/)

  m = line.match(dotenv_pattern)

  if m.nil?
    abort("Invalid entry in .env file. Please verify your .env file is correctly formatted (line: #{index + 1}, value: #{line}).")
  end

  key = m[:key]
  # Ensure string (in case of empty value) and escape any quotes present in the value.
  val = m[:val].to_s.gsub('"', '\"')
  h.merge(key => val)
end

I have the same issue. never happened before. remove and pod install again. my .env file does not have invalid entries. no clue what's happening

1.4.0 is free of this error. Downgrade to 1.4.0

@josevalb Even .env file is valid still it won't take other .env files apart from .env.dev. I have fixed this issue already raised a pr. https://github.com/luggit/react-native-config/pull/539
Meanwhile, you can edit your package.json like this:
"react-native-config": "git+https://github.com/CoffeeBeansLabs/react-native-config.git"
it will work fine 馃憤

Was this page helpful?
0 / 5 - 0 ratings