elixir - 在 Elixir 应用程序中加载 Erlang 项目

标签 elixir elixir-mix

我正在构建一个使用 hackney Erlang 项目的 Elixir 应用程序,但我无法使用 hackney 提供的方法。 我的 mix.exs 看起来像这样:

defmodule Connecter.Mixfile do
  use Mix.Project

  def project do
    [app: :connecter,
     version: "0.0.1",
     elixir: "~> 1.2-dev",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    [{:hackney, "~>1.4.6"}]
  end
end

当我尝试访问库的方法时,我得到UndefinedFunctionError:

$ iex lib/connecter.ex 
Erlang/OTP 18 [erts-7.1] [source-2882b0c] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.2.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :h
heart                  hipe_unified_loader    
iex(1)> :h
heart                  hipe_unified_loader    
iex(1)> :hackney
:hackney
iex(2)> method = :get
:get
iex(3)> URL = 'https://friendpaste.com'
** (MatchError) no match of right hand side value: 'https://friendpaste.com'

iex(3)> url = 'https://friendpaste.com'
'https://friendpaste.com'
iex(4)> headers = []
[]
iex(5)> payload = <<>>
""
iex(6)> options = []
[]
iex(7)> {:ok, status, resp, client} = :hackney.request(method, url, headers, payload, options)
** (UndefinedFunctionError) undefined function :hackney.request/5 (module :hackney is not available)
    :hackney.request(:get, 'https://friendpaste.com', [], "", [])

为什么会发生这种情况?

最佳答案

您需要将 :hackney 作为应用程序的必需模块:

def application do
  [applications: [:logger, :hackney]]
end

参见Applications - OTP Design Principles在 Erlang OTP 文档中。

此外,使用 iex -S mix 启动 IEx 将确保为您的混合依赖项正确设置所有加载路径。

关于elixir - 在 Elixir 应用程序中加载 Erlang 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34025243/

相关文章:

elixir - 无法访问主 elixir 项目中的依赖项配置

elixir - 验证一个日期比另一个日期晚或相同

elixir - 在 Phoenix LiveView 中提交后重置表单输入字段

elixir - 覆盖毒物编码器

testing - 当行为取决于 Application.get_env/3 时如何测试模块?

functional-programming - 如何在函数式编程语言中使用函数而不是提前返回

elixir - 在 Elixir 中搜索文件字节模式的最有效方法

elixir - 解压内部 tarball 失败 : invalid argument

erlang - 如何修复在 Windows 上编译 bcrypt_elixir 或 argon_elixir 的错误?