Vaibhav
Vaibhav

Reputation: 11

Do all the .ex files have to be in /lib folder when creating a mix project?

I generated a new mix project and I can see the .ex file in /lib folder. When trying to run the application from project root path using "mix run " I get an error since the file is in /lib folder. To overcome that I created a new file in the project root having the start(_type,_args) method and I am trying to set that module as the mod key in my mix.exs but it does not link to the specified module.

Is there any other way to run the application from project root?

def application do
  [
    extra_applications: [:logger],
    mod: {Proj,[]}
  ]
end


def Proj do
  use Application

  def start(_type,_args) do
    Proj1.printHello() #calling the method from another module in /lib
  end
end

Here is the error log:

10:55:24.579 [info]  Application proj1 exited: exited in: Proj.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function Proj.start/2 is undefined (module Proj is not available)
            Proj.start(:normal, [])
            (kernel) application_master.erl:277: :application_master.start_it_old/4

Upvotes: 0

Views: 484

Answers (2)

Mr H
Mr H

Reputation: 5304

Just to make what @kamil done a bit more descriptive.

defmodule Proj.MixProject do
  use Mix.Project

  def project do
    [
      # ...
      elixirc_paths: elixirc_paths(Mix.env())
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

Upvotes: 1

Kamil Lelonek
Kamil Lelonek

Reputation: 14784

What you need to do in your mix.exs file is to add:

defmodule Proj.MixProject do
  use Mix.Project

  def project do
    [
      # ...
      elixirc_paths: ["lib"]
    ]
  end
end

Upvotes: 1

Related Questions