noname
noname

Reputation: 565

Elixir app working in iex but fail when try to mix run

I am trying to create application with Elixir, Ecto without Pheonix.

mix.exs

defmodule Wtf.MixProject do
  use Mix.Project

  def project do
    [
      app: :wtf,
      version: "0.1.0",
      elixir: "~> 1.13",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
      mod: {Wtf.Application, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:postgrex, "~> 0.16.3"},
      {:ecto, "~> 3.8.4"},
      {:ecto_sql, "~> 3.8.3"},
      {:json, "~> 1.4"},
      {:jason, "~> 1.3.0"},
      {:ratatouille, "~> 0.5.0"},
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

repo.ex

defmodule Wtf.Repo do
  use Ecto.Repo,
    otp_app: :wtf,
    adapter: Ecto.Adapters.Postgres
end

application.ex

defmodule Wtf.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    children = [
      # Starts a worker by calling: Wtf.Worker.start_link(arg)
      # {Wtf.Worker, arg}
      Wtf.Repo
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Wtf.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

config.exs

import Config

config :wtf, Wtf.Repo,
  database: "wtf_database",
  username: "wtf",
  password: "wtf",
  hostname: "localhost",
  port: "5050",
  log: false

config :wtf, ecto_repos: [Wtf.Repo]

wtf.ex

defmodule Wtf do
  import Ecto.Query, warn: false

  alias Wtf.{Repo, Data}

  def hello do
    Repo.all(WtfData)
  end
end

Below I am attaching list of my files in project like you can see that is just basic setup for querying database. When I run iex -S mix app is loading properly and I can query database all CRUD working fine, when I do Wtf.Repo.all(Wtf.Data) I am getting all 3 rows which I have in database.

But when I am trying to run this app with mix run lib/wtf.ex I am getting an error

function Repo.all/1 is undefined (module Repo is not available)

I was looking in the google how to solve it and it seems like I am doing everything like it should be done. I follow few examples but always the same issue. Can some explain me what is wrong with it? What am I missing?

Upvotes: 0

Views: 876

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

Sidenote: of course you don’t need to build a release unless you want to run a release.

The below part of mix.exs defines the application within your project and its entry point.

def application do
  [
    extra_applications: [:logger, :postgrex, :ecto, :ecto_sql],
#   ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ THIS
    mod: {Wtf.Application, []}
  ]
end

Instead of running an explicit file with mix run ‹file›, do mix run without a parameter, which is, according to docs,

can be used to start the current application dependencies, the application itself, and optionally run some code in its context.

Upvotes: 1

Related Questions